wcf - 升级到.NET 4.5后获取AddressAlreadyInUseException

标签 wcf .net-4.0 configuration .net-4.5

我最近在服务器上安装了新的.NET Framework 4.5(以前安装了4.0),并且在启动公开WCF终结点的Windows服务时得到了System.ServiceModel.AddressAlreadyInUseException

System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:56543. This could happen if there is another application already listening on this endpoint or if you have multiple service endpoints in your service host with the same IP endpoint but with incompatible binding configurations. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.ServiceModel.Channels.SocketConnectionListener.Listen() --- End of inner exception stack trace --- at System.ServiceModel.Channels.SocketConnectionListener.Listen() at System.ServiceModel.Channels.BufferedConnectionListener.Listen() at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener) at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback) at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at Qosit.Infrastructure.UnisServer.OnStart(String[] args)



我的WCF端点的配置如下所示:
<system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBindingConfiguration" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
          <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name="MEX">
          <serviceMetadata/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MEX" name="MyAssembly.MyNamespace.MyService">      
        <endpoint address="net.tcp://localhost:56543/MyService"
          binding="netTcpBinding" bindingConfiguration="NetTcpBindingConfiguration" contract="MyAssembly.MyNamespace.MyServiceInterface" />
        <endpoint address="net.tcp://localhost:56543/MEX" binding="mexTcpBinding"
          contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

我认为这与使用相同端口的MEX端点有关,但是我不确定升级到.NET Framework 4.5后如何正确配置它。

WCF是否进行了更改,以使这些配置引发异常?

最佳答案

这是由于在here中记录的netTcp端点和mex端点使用同一端口方面存在一些限制,请参见“使用NetTcpBinding在服务端点和mex端点之间共享端口”一节。在4.0中,listenBackLogMaxConnections的默认值为10。在4.5中,这些默认值修改为12 * ProcessorCount。当您尝试在netTcpBinding和mex端点之间共享端口时,如果您对这两个属性使用不同的值,则会发生此异常。在4.0中,这很好,因为您已将它们设置为默认值(10),因此这些设置在两个端点上都没有区别。但是在4.5中,对于netTcp端点,这些保留为10,但计算为12 * ProcessorCount。所以异常(exception)。

要解决此问题,有两种方法:

  • 从配置中删除这些设置(listenBackLogMaxConnections)。这样,您将自动获得默认值12 *处理器数,该值大于默认值4.0。
  • 按照documentation
  • 中所述在其他端口上配置mex端点的解决方法

    请查看this blog了解更多详细信息。

    关于wcf - 升级到.NET 4.5后获取AddressAlreadyInUseException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911860/

    相关文章:

    visual-studio-2010 - 为 WCF 服务添加服务引用时出现 VS 2010 错误 “Object reference not set to an instance of an object”

    wcf - 浏览器连接到 WCF 服务,但不连接到我的 WCF 客户端。可能是什么原因?

    c# - .net 对象大小填充?

    spring-mvc - 如何在 nginx 后面为 i18n 使用具有多个域的单个 tomcat 实例

    wcf - 使用 SSL 和 WCF 自托管 - 无法将证书绑定(bind)到端口

    c# - 读取 -> 使用 -> 更新

    windows - 在没有 .NET Framework 4.0 的情况下使用 Windows SDK 7.1 编译 C++ 代码

    java - 如何为我的 NetBeans 模块 Maven 设置集群

    php - 如何在 Ubuntu 15 上的 Apache 服务器上配置虚拟主机

    c# - WCF/LINQ to SQL 中的序列化