2010-05-11 4 views
1

Serveur:Biens immobiliers à Remoting

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

Client:

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

Classe:

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

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

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

Connexion OK, 8080 le port ouvert, sur le côté client remoteHost est non nul, mais remoteHost .Name == ""

Pourquoi?

Répondre

2

Vous devez maréchal votre instance de serveur spécifique (h) dans le canal, sinon une instance par défaut sera créée.

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

+0

insérer RemotingServices.Marshal (h, "Serveur") après avoir enregistré un service bien connu. Rien ne change –

+0

Je suis désolé tout fonctionne correctement. Merci beaucoup –

-1

Vous devez corriger votre classe pour faire le code réel de restituer les propriétés comme indiqué, j'ai ajouté le type de chaîne myHostName variable à utiliser pour les propriétés de 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

lancer exception de connexion –

+0

@ Evl-ntnt ... modifiera cette réponse ... – t0mm13b

+0

à nouveau remoteHost.Name == "" Je pense que problème dans les membres de la classe marshaling –