将远程对象传递给 C# 中的远程方法
我试图将远程对象作为参数传递给远程方法,但当远程对象尝试在接收到的远程对象上运行方法时,我收到安全异常。
这是一个示例远程对象:
public class SharpRemotingDLL : MarshalByRefObject
{
public String getRemoteMessage(SharpRemotingDLL server)
{
// the exception is raised here
return server.getMessage();
}
public String getMessage()
{
return "Hello World!";
}
}
这是服务器启动程序(其两个实例正在运行,一个在 127.0.0.10025 上,另一个在 127.0.0.10026 上):
public static int Main(string[] args)
{
TcpServerChannel channel;
channel = new TcpServerChannel(10025);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SharpRemotingDLL),
"SharpRemotingDLL",
WellKnownObjectMode.Singleton);
Console.ReadLine();
return 0;
}
这是客户端:
static void Main(string[] args)
{
SharpRemotingDLL server0 = (SharpRemotingDLL)
Activator.GetObject(typeof(SharpRemotingDLL),
"tcp://localhost:10025/SharpRemotingDLL");
SharpRemotingDLL servers[1] = (SharpRemotingDLL)
Activator.GetObject(typeof(SharpRemotingDLL),
"tcp://localhost:10026/SharpRemotingDLL");
Console.WriteLine(server0.getRemoteMessage(server1));
}
如何正确地将 server1 作为参数传递给getRemoteMessage 方法?
I'm attempting to pass a remote object as a parameter to a remote method, but I get a security exception when the remote object attempts to run a method on the received remote object.
This is a sample remote object:
public class SharpRemotingDLL : MarshalByRefObject
{
public String getRemoteMessage(SharpRemotingDLL server)
{
// the exception is raised here
return server.getMessage();
}
public String getMessage()
{
return "Hello World!";
}
}
This is the server starter (two instances of this are running, one on 127.0.0.10025, the other on 127.0.0.10026):
public static int Main(string[] args)
{
TcpServerChannel channel;
channel = new TcpServerChannel(10025);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(SharpRemotingDLL),
"SharpRemotingDLL",
WellKnownObjectMode.Singleton);
Console.ReadLine();
return 0;
}
And this is the client:
static void Main(string[] args)
{
SharpRemotingDLL server0 = (SharpRemotingDLL)
Activator.GetObject(typeof(SharpRemotingDLL),
"tcp://localhost:10025/SharpRemotingDLL");
SharpRemotingDLL servers[1] = (SharpRemotingDLL)
Activator.GetObject(typeof(SharpRemotingDLL),
"tcp://localhost:10026/SharpRemotingDLL");
Console.WriteLine(server0.getRemoteMessage(server1));
}
How do I correctly pass server1 as a parameter to the getRemoteMessage method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
vbigiani,
这篇文章应该可以阐明您的问题:
http: //weblogs.asp.net/jan/archive/2003/06/01/8106.aspx
如果这不起作用,您可以在此处获得几个相关的 Google 搜索:
由于安全限制,无法访问 System.Runtime.Remoting.ObjRef 类型
vbigiani,
This article should shed some light on your problem:
http://weblogs.asp.net/jan/archive/2003/06/01/8106.aspx
If that doesn't work, you can get several relevant Google hits here:
Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed
我以前写过这种代码,但从未尝试过以这种方式将一个服务器对象传递给另一个服务器对象。 我不是安全专家,但我可以理解为什么这可能是不允许的。
您应该直接从 Server1 获取消息,而不是要求另一个远程服务器返回它。 换句话说,您的消息检索逻辑需要位于客户端。
I have written this kind of code before, but I have never tried to pass a server object to another server object in this way. I'm not a security expert, but I can sense why this might not be allowed.
You should obtain the message directly from Server1, rather than asking another remote server to return it. In other words, your message retrieval logic needs to be in the client.