检测WCF中的套接字断开连接我们正在构建一个WCF服务器(.NET4.0)。它只会使用net.tcp传输。当客户端关闭TCP连接时,服务器将得到一个未处理的CommunicationException,并终止。Q1。如何处理CommunicationException以便服务器不会终止并继续为其他客户端提供服务?Q2。在处理程序中,如何获取中止会话的SessionId?我需要这样做来清理一些特定于会话的数据。提前致谢!PS连接是通过Internet建立的,因此无论客户端是否正常断开连接,套接字都可以随时关闭。任何WCF通道都实现ICommunicationObject,它为通道生命周期提供事件。您应该收听Faulted事件sessionId始终可以从OperationContext.Current属性访问。当您的客户端打开通道(第一次操作)时,注册相应的事件:OperationContext.Current.Channel.Faulted+=newEventHandler(Channel_Faulted);OperationContext.Current.Channel.Closed+=newEventHandler(Channel_Faulted);并且:以上就是C#学习教程:WCF中检测套接字断开连接的全部内容。,EventArgse){注销((IContextChannel)sender);}protectedvoidLogout(IContextChannelchannel){stringsessionId=null;if(channel!=null){sessionId=channel.SessionId;}[...]}ICommunicationObjectobj=(ICommunicationObject)回调;obj.Closed+=newEventHandler((a,b)=>{if(list.Exists(cli=>cli.CallbackService==(ITecnobelRemoteServiceCallback)a)){varquery=(fromccinlistwherecc.CallbackService==(ITecnobelRemoteServiceCallback)a选择cc).ToList();query.ForEach(delegate(Clientccc){ccc.CallbackService=null;});}});本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:
