Handling Disconnects with WCF
来源:互联网 发布:快乐赚幸运28源码 编辑:程序博客网 时间:2024/05/21 10:55
I've been working on an application which relies heavily on WCF to communicate. I have a very simple interface (contract) that the server supports to connect and disconnect: it exposes the methods Register() and Unregister(). When the client starts up and connects, it calls the Register() method, passing in some identification info. I save this information in a List<T> so that it can be used by the server to provide callbacks. When the client disconnects, it calls Unregister() which then removes the client from the collection.
That works well enough, right up until a client abruptly disconnects (ex. a connection error, network line goes down, etc.) Then suddenly I end up with a reference to a disconnected client on the server. If I attempt to use this connection to send a message back to the client an exception is thrown (which still works OK). It would be nice to know immediately as soon as a client disconnects.
If you are using TCP (full duplex) as the communication channel, you can do this pretty easily - note: this doesn't work for some of the other communication modes, like HTTP duplex. For those, you might have to rely on the various timeout settings in the .config file.
Here's what that ends up looking like (I happen to be taking advantage of LINQ here, but you can easily adjust that code if you're using an older vesion of the framework):
203 private void IServer.Register(string systemName)
204 {
205 IClientCallback remoteMachine = OperationContext.Current.GetCallbackChannel<IClientCallback>();
206 // Hook up events to let us know if the client disconnects w/o telling us.
207 OperationContext.Current.Channel.Faulted += new EventHandler(ClientFaulted);
208 OperationContext.Current.Channel.Closed += new EventHandler(ClientClosed);
209
210 // Get object reference to figure out the IP address of the connected client
211 MessageProperties prop = OperationContext.Current.IncomingMessageProperties;
212 RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
213
214 // (threading/locking code removed)
215
216 client = m_callbackList.Find(c => c.SystemName == systemName);
217 if (client == null)
218 {
219 // It's not already on our list, add it
220 m_callbackList.Add(new RegisteredClient(systemName, endpoint.Address, remoteMachine));
221 }
222 }
When the client disconnects, the ClientFaulted/ClientClosed events will be fired. Honestly, at this point, the Unregister method isn't really needed, assuming you follow the same disconnection process in both cases.
- Handling Disconnects with WCF
- Effective Error Handling with WCF & REST(WCF REST Service 高效错误处理)
- handling faults in WCF
- Error handling in WCF
- Error Handling with exceptions
- Error Handling with Exceptions【1】
- Error Handling with Exceptions【2】
- Error Handling with Exceptions【3】
- Error Handling with Exceptions【4】
- Error Handling with Exceptions【5】
- Handling Configuration Changes with Fragments
- Handling Configuration Changes with Fragments
- Handling Configuration Changes with Fragments
- RESTful Error Handling with Spring
- Simple Calculator with Exeption Handling
- Exception Handling in WCF using Fault Contract
- 如何在WCF进行Exception Handling
- 第九章: Error Handling with Exceptions
- 3月8日的Google不能访问的DNS故障,只影响了固定宽带用户,但手机和无线依然能上网
- C#按钮事件中有循环,用另一个按钮控制停止,暂停,继续程序执行
- oracle基础整理一
- 聚焦WCF行为的扩展
- lua环境配置
- Handling Disconnects with WCF
- Player/Stage 安装官方指南|Player 2.1.1 / Stage 3.0.* (official release)
- 对不起你
- 今天出差去石家庄,明天转去济南
- 图像旋转系列文章的源代码下载
- 系统程序员成长计划-容器与算法(一)(上)
- 还哈
- Mapx常见问题
- 改变Windows资源管理器的默认路径