Windows服务中托管的WCF服务将无法正常运行

时间:2011-12-15 14:37:46

标签: c# .net wcf tcp windows-services

我实际上有两个不同的问题,这取决于我尝试运行看起来相关但以不同方式显示自己的服务的方式。请注意,我运行的代码是Microsofts guide中使用的代码的精确副本,但不同的名称空间和略有不同的类名称除外。

当我尝试使用此方法运行服务时,Windows服务已成功启动,但是当它启动时,会弹出WCF服务主机框并显示一条错误消息,指出IP端点localhost上已有一个侦听器:当我以这种方式运行时,这是我的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="CfmaWcfEphemerisLibrary.ServiceBehavior"
               name="CfmaWcfEphemerisLibrary.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="CfmaWcfEphemerisLibrary.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8529/Service1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CfmaWcfEphemerisLibrary.ServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

战斗了一段时间后,我发现了一个建议,即在tcp连接上启用端口共享可能有所帮助。我试过了,但是当我尝试启动服务时它失败了,在“应用程序”下的Windows事件日志中,我收到一条错误消息:

  

无法启动服务。 System.ServiceModel.AddressAlreadyInUseException:IP端点0.0.0.0:8529上已有一个侦听器。确保您没有尝试在应用程序中多次使用此端点,并且没有其他应用程序在此端点上进行侦听。 ---&GT; System.Net.Sockets.SocketException:通常只允许使用每个套接字地址(协议/网络地址/端口)      在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)      在System.Net.Sockets.Socket.Bind(EndPoint localEP)      在System.ServiceModel.Channels.SocketConnectionListener.Listen()      ---内部异常堆栈跟踪结束---      在System.ServiceModel.Channels.SocketConnectionListener.Listen()      在System.ServiceModel.Channels.BufferedConnectionListener.Listen()      在System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()      在System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)      在System.ServiceModel.Channels ....

我无法弄清楚为什么我在启用了共享的端口上获得了一个端口使用异常。当我尝试在启用端口共享的情况下运行服务时,这是我的App.config文件。

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="CfmaWcfEphemerisLibrary.ServiceBehavior"
               name="CfmaWcfEphemerisLibrary.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpBinding"
          contract="CfmaWcfEphemerisLibrary.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8529/Service1" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CfmaWcfEphemerisLibrary.ServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netTcpBinding>
        <binding portSharingEnabled="true" name="tcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security>
            <transport>
              <extendedProtectionPolicy policyEnforcement="Never" />
            </transport>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:0)

使用相同的端口进行元数据交换可能会出现问题: http://msdn.microsoft.com/en-us/library/aa702636.aspx 请参阅“使用NetTcpBinding在服务端点和mex端点之间共享端口”部分。

总之,这解决了这个问题: &lt; endpoint address =“net.tcp:// localhost:8530 / mex”...&gt;