0

我的症状与这篇文章描述的完全一样http://social.msdn.microsoft.com/Forums/vstudio/en-US/302ca96e-a810-4958-9905-90ba1175765d/servicehost-does-not-detech-endpoints-cant-从故障状态恢复

我想知道这是否是一个已知的错误。

我的代码与 Dave 的代码略有不同,而他的 ServiceHost 实例(名为 WebService)在 Start() 方法之外。我的 ServiceHost 实例(命名主机)在里面声明。调试时,我在主机描述中检查端点地址已更改为正确的 IP。但是,Open() 仍然会使用旧的错误 IP 地址引发异常。

    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

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
 ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]

和接口的 ServiceContract

[ServiceContract(CallbackContract = typeof(IPtiCommunicationCallback), SessionMode = SessionMode.Required)]

app.config 中没有其他配置。一切都在代码中。

谢谢

更新:

我发现我们俩都对 ServiceHost Constructor (Object, Uri[]) 使用了相同的重载,这使得 Web 服务成为一个单例。

当我们使用相同的单例创建新的服务主机时,以某种方式更改端点地址不会产生影响,因为即使主机已中止,服务的实例仍然存在。

当我们创建新主机时,有什么解决方案可以清理那个单例吗?

到目前为止,这是我的怀疑,如果我错了,请纠正我。

4

1 回答 1

0

显然 WCF 缓存了一些套接字信息。我发现的最佳解决方法是 Ivan 在这里https://stackoverflow.com/a/6839265/955400给出的。在尝试调用 host.Open() 之前检查是否可以打开连接。

于 2020-03-27T15:50:07.173 回答