我有一个2 WCF服务,托管在winform应用程序中:1暴露一个基本的http绑定,1暴露IClientAccessService(webhttpbinding)和一个带有PollingDuplexHttpBinding和NTLM安全性的接口,用于silverlight 5客户端。在某些情况下,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;
}