2009-05-31 115 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