简体   繁体   English

如何在WCF服务上检测到客户端刚刚断开连接?

[英]How to detect on a WCF Service that a client just got disconnected?

I'm handling the channel faulting and closing event as below 我正在处理以下通道故障和关闭事件

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
                    ConcurrencyMode = ConcurrencyMode.Multiple,
                    UseSynchronizationContext = false)]

 OperationContext.Current.Channel.Faulted += Channel_Faulted;
 OperationContext.Current.Channel.Closed += Channel_Closed;

 private void Channel_Faulted(object sender, EventArgs e)
 {
       //perform Something
 }

 private void Channel_Closed(object sender, EventArgs e)
 {
        //perform Something
 }

The binding info is as below. 绑定信息如下。

NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true);
tcpBinding.ReceiveTimeout = new TimeSpan(48, 0, 0);
tcpBinding.ReliableSession.Enabled = true;
tcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(0, 0, 10);

The event handlers get hit when i gracefully end the client by closing/aborting the proxy but the event handlers do not get called when i kill the client process through the Task Manager or shutdown the system on which the client is running. 当我通过关闭/终止代理来优雅地结束客户端时,事件处理程序会受到攻击,但是当我通过任务管理器终止客户端进程或关闭正在运行客户端的系统时,事件处理程序不会被调用。 What am i doing wrong here? 我在这里做错了什么? Any code example to handle this situation will be helpful, thanks. 谢谢任何处理这种情况的代码示例。

Is this an XY problem? 这是XY问题吗? If your service instancing is PerCall it won't matter because connections will open and close for each method call to the service. 如果您的服务实例PerCall ,则没有关系,因为对于该服务的每个方法调用,连接都会打开和关闭。

Service faulting only occurs as a result of using a WCF conduit. 服务故障仅是由于使用WCF导管而发生的。 Killing the process will sever the underlying TCP connection. 终止进程将切断基础的TCP连接。 Unless your server is in the middle of using the channel it won't fault. 除非您的服务器正在使用该通道,否则不会出错。

What I generally like to do with my per-session WCF services is mark them IDisposable then handle any cleanup tasks in the Dispose regardless of graceful termination. 我通常希望对每个会话的 WCF服务执行的操作是将它们标记为IDisposable然后处理Dispose中的任何清理任务,而不管是否正常终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM