C# ServiceHost在WCF self host中首次失败时,无法使用新ServiceHost实例再次打开()
我的症状和这篇文章描述的一模一样 我想知道这是否是一个已知的错误 我的代码与Dave的略有不同,而他的ServiceHost实例(名为WebService)在Start()方法之外。我的ServiceHost实例(命名主机)在内部声明。调试时,我在主机描述中签入端点的地址是否已更改为正确的IP。但是,Open()仍然抛出一个旧的错误IP地址的异常C# ServiceHost在WCF self host中首次失败时,无法使用新ServiceHost实例再次打开(),c#,wcf,.net-4.5,C#,Wcf,.net 4.5,我的症状和这篇文章描述的一模一样 我想知道这是否是一个已知的错误 我的代码与Dave的略有不同,而他的ServiceHost实例(名为WebService)在Start()方法之外。我的ServiceHost实例(命名主机)在内部声明。调试时,我在主机描述中签入端点的地址是否已更改为正确的IP。但是,Open()仍然抛出一个旧的错误IP地址的异常 private bool InitHost(PtiType type, string serverIp, int portNumber)
private bool InitHost(PtiType type, string serverIp, int portNumber)
{
if (!HostDictionary.ContainsKey(type))
{
Uri addressBase = new Uri(String.Format("net.tcp://{0}:{1}/CommunicationService/{2}", serverIp, portNumber.ToString(), type.ToString()));
var service = new PtiCommunicationService(type);
service.ClientConnected += service_ClientConnected;
service.ClientBroadcasted += service_ClientBroadcasted;
service.ClientSentTo += service_ClientSentTo;
service.ClientDisconnected += service_ClientDisconnected;
var host = new ServiceHost(service, addressBase);
//For publishing metadata only
//Define Metadata endPoint, So we can publish information about the service
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(mBehave);
//Enable debug info in fault
((ServiceDebugBehavior)host.Description.Behaviors[typeof(ServiceDebugBehavior)]).IncludeExceptionDetailInFaults=true;
host.AddServiceEndpoint(typeof(IPtiCommunication), new NetTcpBinding(SecurityMode.None), "");
host.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(),
"mex");
try
{
host.Open();
//Add host to dictionary to keep track
HostDictionary.Add(type, host);
LogList.Add(String.Format("{0}\tThe service {1} at {2} is ready", DateTime.Now.ToLongTimeString(), service.ServiceType.ToString(), serverIp));
string hostInfo = String.Format("{0}\tHost information:\n", DateTime.Now.ToLongTimeString());
hostInfo += "Enpoints details:\n";
foreach (var endpt in host.Description.Endpoints)
{
hostInfo += String.Format("\t Name:\t\t{0}\n", endpt.Name);
hostInfo += String.Format("\t Logical address:\t{0}\n", endpt.Address);
hostInfo += String.Format("\t Physical address:\t{0}\n", endpt.ListenUri);
hostInfo += String.Format("\t Binding:\t\t{0}\n", endpt.Binding);
hostInfo += String.Format("\t Contract:\t{0}\n\n", endpt.Contract.ContractType.Name);
}
LogList.Add(hostInfo);
}
catch (Exception e)
{
host.Abort();
host = null;
LogList.Add(String.Format("{0}\t{1}", DateTime.Now.ToLongTimeString(), e.Message));
}
return true;
}
return false;
}
以下是我的PtiCommunicationService服务行为
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
和接口的ServiceContract
[ServiceContract(CallbackContract = typeof(IPtiCommunicationCallback), SessionMode = SessionMode.Required)]
app.config中没有其他配置。所有这些都在代码中
谢谢
更新:
我发现我们两人对ServiceHost构造函数(Object,Uri[])使用相同的重载,这构成了web服务的单例
当我们使用相同的单例创建新的服务主机时,不知何故,更改端点地址不会产生影响,因为即使主机已中止,服务实例仍在那里
当我们创建新主机时,有什么解决方案来清理这个单例吗
到目前为止,这是我的怀疑,如果我错了,请纠正我。显然WCF缓存了一些套接字信息。我发现最好的解决方法是Ivan在这里给出的。在尝试调用host.open()之前,请检查是否可以打开连接。是否可以粘贴从.open()调用的错误?@ilansch这是我得到的异常:在IP端点=192.168.75.77:3200上侦听时发生TCP错误(10049:请求的地址在其上下文中无效)。这是一个预期的例外。出乎意料的是,当我使用新的正确IP创建新主机时,当我调用Open()时,它仍会抛出该异常