2009-05-31 57 views
0

我试图将远程对象作为参数传递给远程方法,但当远程对象尝试在接收到的远程对象上运行方法时遇到安全异常。将远程对象传递给远程方法在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.10026运行,一个在127.0.0.10025,其他):

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方法?

回答

0

我以前写过这种代码,但我从来没有试过用这种方法将服务器对象传递给另一个服务器对象。我不是安全专家,但我可以理解为什么这可能不被允许。

您应该直接从Server1获取消息,而不是让另一个远程服务器返回它。换句话说,你的消息检索逻辑需要在客户端。

+0

这只是一些示例代码来演示问题。在我的真实代码中,SharpRemotingDLL是一个导出基本文件操作(CreateFile,ReadFile等)的类,我使用类似的合成器来实现两个服务器之间的CopyFile和MoveFile: 客户端使用 server1.CopyFile(“foo。 TXT”,服务器2) 的CopyFile方法: 公共无效的CopyFile(字符串名称,SharpRemotingDLL服务器) { server.WriteFile(姓名,this.ReadFile(名)); } – vbigiani 2009-05-31 17:14:26

+0

错误信息的确切用词是什么? – 2009-05-31 17:37:29