c# - Windows 服务中托管的 WCF 服务无法正常运行

标签 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 事件日志中我收到一条错误消息:

Service cannot be started. System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:8529. Make sure that you are not trying to use this endpoint multiple times in your application and that there are no other applications listening on this endpoint. ---> 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....

我不明白为什么在启用共享的端口上出现端口使用异常。这是我尝试在启用端口共享的情况下运行服务时的 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>

最佳答案

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

简而言之,这解决了问题: <端点地址="net.tcp://localhost:8530/mex"...>

关于c# - Windows 服务中托管的 WCF 服务无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8521762/

相关文章:

c# - 如何将整个字符串与正则表达式匹配?

c# - CMake生成项目时如何设置C#项目的语言版本?

c# - 使用 SqlFileStream 从 WCF 服务返回流

c# - 在 WCF 服务中使用大量数据后清理内存

.net - protobuf -net 不知道如何序列化类型为 object 的数据成员

c# - 处理后SqlDataReader记录的使用

c# - 隐藏下拉列表中的项目 - C#

.net - 是否可以从.net webservices返回接口(interface),或者只能返回具体类?

c# - 委托(delegate)分配是否在 C# 中创建新副本?

c# - Visual Studio 2010 中的 Word 2007 文档