如何强制net.tcp mex端点(mexTcpBinding)参与端口共享?

时间:2010-11-12 15:22:20

标签: wcf net.tcp mex-bindings

我有一个WCF服务,它作为Windows服务托管。我们想在同一地址启用一个mex端点(但后缀为'/ mex')。我一直在尝试使用以下配置执行此操作(失败):

<system.serviceModel>

  <services>
    <service
      name="MyCompany.MyService"
      behaviorConfiguration="defaultServiceBehavior">

      <host>
        <baseAddresses>
          <add baseAddress="net.tcp://localhost"/>
        </baseAddresses>
      </host>

      <endpoint
        address="MyService"
        binding="netTcpBinding"
        contract="MyCompany.IMyService"
        bindingConfiguration="netTcpBindingConfig"
        />

      <endpoint
        address="MyService/mex"
        binding="mexTcpBinding"
        contract="IMetadataExchange"
        />

    </service>
  </services>

  <behaviors>
    <serviceBehaviors>
      <behavior name="defaultServiceBehavior">
        <serviceMetadata />
      </behavior>
    </serviceBehaviors>
  </behaviors>

  <bindings>
    <netTcpBinding>
      <binding name="netTcpBindingConfig" portSharingEnabled="true" />
    </netTcpBinding>
  </bindings>

</system.serviceModel>

运行时,服务主机抛出AddressAlreadyInUseException抱怨“IP端点0.0.0.0:808上已有监听器”。这实际上对我有意义,因为端口共享服务已打开该端口,以便为MyService端点以及请求在此计算机上共享该端口的任何其他服务提供服务。

所以似乎mex端点想要对端口808进行exlusive访问。我可以通过调整mex端点来解决这个问题:

<endpoint
  address="net.tcp://localhost:818/MyService/mex"
  binding="mexTcpBinding"
  contract="IMetadataExchange"
  />

这意味着mex端点现在拥有自己的专用端口。这方面的缺点是,任何其他想要公开mex端点的服务也需要一个唯一的端口用于其mex端点。这使得在查找mex端点时非常难以预测。

有没有办法强制mex端点参与端口共享?

1 个答案:

答案 0 :(得分:8)

两个选项:

  1. 简单方法:将mex指向的整个绑定更改为netTcpBinding,并让它重用您的bindingConfiguration。 mexTCPBinding只是为了方便而且是可选的。如果它不适合您,请不要使用它。

  2. 困难的方法:您可以修改mexTCPBinding以启用共享。我见过的唯一例子是代码:http://blogs.msdn.com/b/drnick/archive/2006/08/23/713297.aspx