当进程被杀死时,WCF tcp 连接保持打开状态 [英] WCF tcp connections stay open when process is killed
本文介绍了当进程被杀死时,WCF tcp 连接保持打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里看到过类似的问题,但找不到答案.我有一个应用程序,它使用 WCF 打开到远程地址的连接,有时当我从任务管理器终止应用程序或应用程序不正常关闭时,连接保持打开状态,然后当我重新启动我的应用程序时,我收到一个异常,告诉我已经存在此端口上的侦听器.
I've seen similar questions here but couldn't find an answer. i have an app that uses WCF to open a connection to remote address, sometimes when i kill the app from the task manager or the app closes ungracefully the connection stays open and then when i restart my app i get an exception telling me there is already a listener on this port.
几个问题:
- 为什么在我终止进程后这些连接保持打开状态?
- 当进程异常关闭时,我如何关闭此连接?
- 如何在尝试创建新连接之前关闭连接?
服务端:
var url = Config.GetRemoteServerUrl();
var binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
binding.ReliableSession.Enabled = Config.RelaiableSession;
binding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
binding.MaxConnections = Config.MaxConcurrentSessions;
binding.ReaderQuotas.MaxArrayLength = Config.ReaderQuotasMaxArrayLength;
binding.MaxReceivedMessageSize = Config.MaxReceivedMessageSize;
binding.SendTimeout = new TimeSpan(0,0, 0, 0,Config.SendTimeout);
binding.OpenTimeout = new TimeSpan(0,0, 0, 0,Config.OpenTimeout);
host = new ServiceHost(ServerFacade.Instance, new Uri[] { new Uri(url) });
host.AddServiceEndpoint(typeof(ITSOServiceContract), binding, url);
host.Open();
serverFacade = host.SingletonInstance as IServerFacade;
推荐答案
您可以尝试添加 Channel_Closed 事件处理程序并使用 Abort() 方法来强制处理它.
You can try to add Channel_Closed event handler and use Abort() method to force it to dispose.
OperationContext.Current.Channel.Closed += channelClosed;
void Channel_Closed(object sender, EventArgs e)
{
var success = false;
try
{
proxy.Close();
success = true;
}
finally
{
if (!success) proxy.Abort();
}
}
这篇关于当进程被杀死时,WCF tcp 连接保持打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文