我在这里看到过类似的问题,但找不到答案。我有一个应用程序使用 WCF 打开与远程地址的连接,有时当我从任务管理器中终止应用程序或应用程序不正常关闭时,连接保持打开状态,然后当我重新启动应用程序时,我收到一个异常,告诉我已经存在此端口上的侦听器。
几个问题:
- 为什么在我终止进程后此类连接保持打开状态?
- 当进程不正常关闭时,如何关闭此连接?
- 在尝试创建新连接之前如何关闭连接?
服务端:
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;