2010-05-11 113 views
1

服務器:屬性的遠程處理

Host h = new Host(); 
h.Name = "JARR!!"; 
TcpChannel channel = new TcpChannel(8080); 
ChannelServices.RegisterChannel(channel); 
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Host), "Server", 
       WellKnownObjectMode.Singleton); 

客戶:

TcpChannel chan = new TcpChannel(); 
      ChannelServices.RegisterChannel(chan); 
      remoteHost = (Host)Activator.GetObject(typeof(Host), 
"tcp://127.0.0.1:8080/Server"); 

類:

[Serializable] 
    public class Host: MarshalByRefObject 
    { 
     public string Name{get; set;} 
     public Host(){} 

     public Host(string n) 
     { 
      Name = n; 
     } 

     public override string ToString() 
     { 
      return Name; 
     } 
    } 

連接OK,8080口岸開通後,客戶端側遠程主機不爲空,但REMOTEHOST .Name ==「」

爲什麼?

回答

2

您需要編組的屬性變量myHostName字符串類型您的特定服務器實例(h)插入到通道中,否則會創建一個默認的服務器實例。

System.Runtime.Remoting.RemotingServices.Marshal(...);

+0

插入RemotingServices.Marshal(H, 「服務器」 );註冊知名服務後。沒有任何變化 – 2010-05-11 13:06:52

+0

對不起,所有作品都是正確的。非常感謝 – 2010-05-11 13:09:16

-1

你需要修復你的課上做實際的代碼返回屬性如圖所示,我已經加入到被用於Name

 
[Serializable] 
public class Host: MarshalByRefObject 
{ 
    private string myHostName; 

    public string Name{ 
     get{ return this.myHostName; } 
     set{ this.myHostName = value; } 
    } 

    public Host(string n) 
    { 
     this.myHostName = n; 
    } 
    public override string ToString() 
    {   
     return this.myHostName; 
    } 
} 
+0

拋出連接異常 – 2010-05-11 11:15:11

+0

@的Evl-ntnt ...將修改這個答案... – t0mm13b 2010-05-11 12:01:45

+0

再次 remoteHost.Name == 「」 我想在類成員問題編組 – 2010-05-11 12:20:21