我在实现双工通讯duplex的时候,service端由这样的一个方法
代码
public void GetPeronBehavior(object sender, ChatEventArgs e)
        {
            MessageType type 
= e.MessageType;
            
switch (type)
            {
                
case MessageType.Enter:
                    _callBack.UserEnter(e.FromPerson);
                    _callBack.GetOnlineUser(
this.GetOnlinePerson());
                    
break;
                
case MessageType.PublicChat:
                    _callBack.ReceivePublicChatInfo(e.FromPerson, e.Chat);
                    
break;
                
case MessageType.Leave:
                    _callBack.UserLeave(e.FromPerson);
                    _callBack.GetOnlineUser(
this.GetOnlinePerson());
                    
break;
                
case MessageType.PrivateChat:
                    _callBack.ReceivePrivateChatInfo(e.FromPerson, e.ToPerson, e.Chat);
                    
break;
            }
        }

 

其中在case MessageType.Enter:中有两个回调客户端的方法UserEnter和GetOnlineUser,然后客户端这样调用:

 

 using (ChatClient chat = new ChatClient(new InstanceContext(this), "dualendpoint"))
            {

                PersonInfo info 
= new PersonInfo();
                info.Age 
= 27;
                info.Name 
= textBox2.Text;
                info.Sex 
= "";
                chat.Contect(info);
            }

 

然后启动服务调用的时候,在服务端回调的方法GetOnlineUser除报错:

The sequence has been terminated by the remote endpoint. The user of the remote endpoint's reliable session expects no more messages and a new message arrived. Due to this the reliable session cannot continue. The reliable session was faulted.

我的想法应该是这样的:

session应该是在服务端回调执行完UserEnter后session被dispose了。所以后续的GetOnlineUser执行的时候就会报上面的错误。

 

解决方案如下

我在接口的connect方法上的OperationContract特性中加入IsInitiating=true

 

[OperationContract(IsOneWay = true,IsInitiating=true)]

void Contect(PersonInfo info);

然后在回调接口中的GetOnlineUser方法上的OperationContract特性中加入IsTerminating=true

[OperationContract(IsOneWay = true,IsTerminating=true)]
        void GetOnlineUser(List<PersonInfo> list);

客户端的代码改为:

 

代码
ChatClient chat = new ChatClient(new InstanceContext(this), "dualendpoint");
                PersonInfo info 
= new PersonInfo();
                info.Age 
= 27;
                info.Name 
= textBox2.Text;
                info.Sex 
= "";
                chat.Contect(info);

 

 

重启启动再看结果,还是报错:

The contract 'IChat' is not self-consistent -- it has one or more IsTerminating or non-IsInitiating operations, but it does not have the SessionMode property set to SessionMode.Required.  The IsInitiating and IsTerminating attributes can only be used in the context of a session.

 

错误提示要加入SessionMode property 然后设置为 SessionMode.Required,ok,加入,

 [ServiceContract(Namespace = "http://cyclonechat.com/", CallbackContract = typeof(ICallBack),SessionMode=SessionMode.Required)]
 public interface IChat

再启动,Ok,大功告成!

 

 

 

 

posted on 2010-07-27 12:22  高山流水2012  阅读(791)  评论(0编辑  收藏  举报