我有 2 个 WCF 服务,托管在一个 winform 应用程序中:1 个公开基本 http 绑定,1 个公开 IClientAccessService (webhttpbinding) 和一个具有 PollingDuplexHttpBinding 的接口和用于 silverlight 5 客户端的 NTLM 安全性。在某些情况下,pollingduplex 端点未正确打开,导致无法连接客户端。为了能够检测到这一点,浏览器应用程序中的 Silverlight 正在轮询接口,当收到响应时,它会通过 tcp 套接字通知服务。
当我没有收到浏览器外应用程序的通知时,我想重新启动 wcf 服务。通过在两个服务上调用 servicehost.close(),然后在 servicehost.open() 之后调用。
奇怪的是使用 basichttp 绑定打开/关闭服务成功,但是使用轮询双工绑定打开服务失败并出现 Channeldispatcher 异常:URI 的注册已经存在,http://localhost:18524/IService/
因为端口仍然被关闭和中止声明服务主机。
我一直在玩 pollingduplex 通道的打开/关闭超时,延迟关闭/打开之间的时间,但没有效果。
有人知道如何释放 pollingduplex 连接的端口吗?
感谢帮助。
打开关闭代码: public void StartServices() { StopServices();
try
{
var configSyncService = new ConfigurationSyncService();
configSyncService.Open();
var mainService = new Service();
mainService.Open();
serviceHosts.Add(configSyncService);
serviceHosts.Add(mainService);
}
catch (Exception ex)
{
string msg = String.Format("Exception starting WCF service for Centrale: {0}", ex.Message);
LOG.Fatal(
LogProperties.Bedienpost,
ex,
Resources.ERROR_STARTING_WCF,
ex.Message);
throw ex;
}
}
public void StopServices()
{
try
{
var result = Parallel.ForEach(serviceHosts, service =>
{
if ((service != null) && (service.State == CommunicationState.Opened))
{
try
{
service.Close();
}
finally
{
service.Dispose();
service = null;
}
}
else if ((service != null) && (service.State != CommunicationState.Faulted))
{
service.Dispose();
service = null;
}
});
if (result.IsCompleted)
{
serviceHosts.Clear();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
轮询双工通道的配置:
private Binding CreateCustomPollingDuplexBinding()
{
CustomBinding binding = new CustomBinding();
binding.Name = "pollingDuplexHttpBinding";
binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
binding.SendTimeout = new TimeSpan(0, 5, 0);
binding.OpenTimeout = new TimeSpan(0, 5, 0);
binding.CloseTimeout = new TimeSpan(0, 5, 0);
binding.Elements.Add(new PollingDuplexBindingElement(PollingDuplexMode.SingleMessagePerPoll));
TextMessageEncodingBindingElement textEncoding = new TextMessageEncodingBindingElement();
textEncoding.ReaderQuotas.MaxArrayLength = int.MaxValue;
textEncoding.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
textEncoding.ReaderQuotas.MaxDepth = int.MaxValue;
textEncoding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
textEncoding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
binding.Elements.Add(textEncoding);
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
httpTransport.MaxBufferPoolSize = int.MaxValue;
httpTransport.MaxBufferSize = int.MaxValue;
httpTransport.MaxReceivedMessageSize = int.MaxValue;
httpTransport.AuthenticationScheme = AuthenticationSchemes.Ntlm;
binding.Elements.Add(httpTransport);
return binding;
}