问题描述
服务端:[ServiceContract(SessionMode=SessionMode.Required,CallbackContract=typeof(IQQSback))]publicinterfaceIQQS{[OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]voidSpeak(stringvalue);[OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]voidWhisper(stringname,stringvalue);[OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]voidLeave();[OperationContract(IsOneWay=false,IsInitiating=true,IsTerminating=false)]List<string>join(stringvalue);}interfaceIQQSback{[OperationContract(IsOneWay=true)]voidReceive(stringsenderName,stringmessage);[OperationContract(IsOneWay=true)]voidReceiveWhisper(stringsenderName,stringmessage);[OperationContract(IsOneWay=true)]voidUserEnter(List<string>name);[OperationContract(IsOneWay=true)]voidUserLeave(stringname);}[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]publicclassService1:IQQS{IQQSbackQQback=OperationContext.Current.GetCallbackChannel<IQQSback>();delegatevoiduserenter(List<string>username);staticeventuserenterUserEnterEvent;delegatevoiduserspeak(stringname,stringcontent);staticeventuserspeakUserSpeakEvent;delegatevoiduserWhisper(stringname,stringcontent);staticSortedList<string,userWhisper>Userspeaklist=newSortedList<string,userWhisper>();stringName;publicList<string>join(stringvalue){if(Userspeaklist.Keys.Contains(value)){returnnewstring[]{"已经有此用户"}.ToList();}this.Name=value;UserEnterEvent+=newuserenter(UserEnterEventFunction);if(UserEnterEvent!=null){UserEnterEvent(newstring[]{value}.ToList());}UserSpeakEvent+=newuserspeak(UserSpeakEventFunction);Userspeaklist.Add(value,UserWhisperFunction);returnUserspeaklist.Keys.ToList();}publicvoidSpeak(stringvalue){if(UserSpeakEvent!=null){UserSpeakEvent(this.Name,value);}}publicvoidWhisper(stringname,stringvalue){if(!Userspeaklist.Keys.Contains(name)){return;}Userspeaklist[name](name,value);}publicvoidLeave(){UserEnterEvent-=UserEnterEventFunction;UserSpeakEvent-=UserSpeakEventFunction;Userspeaklist.Remove(this.Name);QQback.UserLeave(this.Name);}voidUserEnterEventFunction(List<string>username){this.QQback.UserEnter(username);}voidUserSpeakEventFunction(stringname,stringcontent){QQback.Receive(name,content);}voidUserWhisperFunction(stringname,stringcontent){QQback.ReceiveWhisper(name,content);}}
服务端配置文件:<?xmlversion="1.0"encoding="utf-8"?><configuration><system.serviceModel><behaviors><serviceBehaviors><behaviorname="metadataBehavior"><serviceMetadatahttpGetEnabled="true"httpGetUrl="http://192.168.0.129:8070/Service1/metadata"/></behavior></serviceBehaviors></behaviors><bindings><wsDualHttpBinding><bindingname="BasicBinding"receiveTimeout="00:30:00"sendTimeout="00:30:00"maxReceivedMessageSize="2147483647"><readerQuotasmaxStringContentLength="2147483647"maxArrayLength="2147483647"/></binding></wsDualHttpBinding></bindings><services><servicebehaviorConfiguration="metadataBehavior"name="WCFDuplex.Service1"><endpointaddress="http://192.168.0.129:8070/Service1"binding="wsDualHttpBinding"bindingConfiguration="BasicBinding"name="TrialInfo"contract="WCFDuplex.IQQS"/></service></services></system.serviceModel></configuration>
客户端:publicpartialclassForm1:Form,QQService.IQQSCallback{publicvoidReceive(stringsenderName,stringmessage){this.listBox2.Items.Add(senderName+"说:"+message);}publicvoidReceiveWhisper(stringsenderName,stringmessage){this.listBox2.Items.Add(senderName+"悄悄对您说:"+message);}publicvoidUserEnter(string[]name){this.listBox1.Items.Add(name[0]);}publicvoidUserLeave(stringname){this.listBox1.Items.Remove(name[0]);}QQService.QQSClientclt=null;publicForm1(){InitializeComponent();InstanceContextins=newInstanceContext(this);clt=newQQService.QQSClient(ins);}privatevoidbutton1_Click(objectsender,EventArgse){string[]ss=clt.join(this.textBox1.Text);if(ss[0]=="已经有此用户"){MessageBox.Show("已经有此用户!");return;}this.button1.Enabled=false;this.textBox1.Enabled=false;this.Text="登陆成功";this.listBox1.Items.AddRange(ss);this.groupBox1.Enabled=true;}}
客户端配置文件:<?xmlversion="1.0"encoding="utf-8"?><configuration><startup><supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5.2"/></startup><system.serviceModel><bindings><wsDualHttpBinding><bindingname="TrialInfo"closeTimeout="00:01:00"openTimeout="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"bypassProxyOnLocal="false"hostNameComparisonMode="StrongWildcard"maxBufferPoolSize="2147483647"maxReceivedMessageSize="2147483647"messageEncoding="Text"textEncoding="utf-8"useDefaultWebProxy="true"/></wsDualHttpBinding></bindings><client><endpointaddress="http://192.168.0.129:8070/Service1"binding="wsDualHttpBinding"bindingConfiguration="TrialInfo"contract="QQService.IQQS"name="TrialInfo"></endpoint></client></system.serviceModel></configuration>
但是我每次执行的时候就会报出异常:因为第一次学习WCF双工,请问下这个是什么情况,应该如何解决呢?
解决方案
解决方案二:
有没有人来帮帮忙啊
解决方案三:
客户端跟服务器端要同时运行,在服务器端要跟踪调试join方法一次,看看执行了哪些代码。至少你要在returnUserspeaklist.Keys.ToList();这条语句这里调试一次。