问题描述
我使用Remoting通讯,Client发送数据给Server,Server加入List,Client可以查询服务器某个Name对应的ID。Server将Client发送过来的TalkerClass寄存在内存中,便于Client查询。server与client之间不要使用共享内容。现在,我Client发送数据给服务器,控制台能显示发送的记录,但是Client查询Server上的Id不到结果。请帮忙解决?RemotingModelpublicclassTalker:MarshalByRefObject{List<TalkerClass>_lstTalkerClass=newList<TalkerClass>();publicvoidRegiste(TalkerClasstc){_lstTalkerClass.Add(tc);System.Console.WriteLine(string.Format("{0}:Guid{1};User:{2}",_lstTalkerClass.Count,tc.Guid,tc.Name));}publicstringSeach(stringuser){foreach(TalkerClasslisin_lstTalkerClass){if(lis.Name.Equals(lis))returnlis.Guid;}return"未注册";}}[Serializable]publicclassTalkerClass{publicstringGuid{get;set;}publicstringName{get;set;}}
RemotingServerstaticvoidMain(string[]args){//注册通道TcpServerChannelchannel=newTcpServerChannel("TalkChannel",8090);//端口随便取ChannelServices.RegisterChannel(channel,true);//注册远程对象RemotingConfiguration.RegisterWellKnownServiceType(typeof(Talker),"Talker",WellKnownObjectMode.SingleCall);Console.ReadLine();}
RemotingClientpublicpartialclassForm1:Form{privateTalker_talk=null;publicForm1(){InitializeComponent();this.Load+=Form1_Load;button1.Click+=btnSend_Click;}privatevoidbtnSend_Click(objectsender,EventArgse){try{TalkerClasstc=newTalkerClass();tc.Guid=Guid.NewGuid().ToString();tc.Name=txtSenderUser.Text;//操作远程对象_talk.Registe(tc);//txtContent.Text="发送成功"+txtWord.Text.Trim();}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidForm1_Load(objectsender,EventArgse){try{//注册通道TcpClientChannelchannel=newTcpClientChannel();ChannelServices.RegisterChannel(channel,true);//获取远程对象_talk=(Talker)Activator.GetObject(typeof(Talker),"TCP://localhost:8090/Talker");}catch(Exceptionex){MessageBox.Show(ex.Message);}}privatevoidbtnReceiver_Click(objectsender,EventArgse){try{//操作远程对象txtReceiverID.Text=_talk.Seach(txtReceiverUser.Text);}catch(Exceptionex){MessageBox.Show(ex.Message);}}}