11

I have a WCF service which is hosted as a Windows Service. We would like to enable a mex endpoint at the same address (but with a '/mex' suffix). I have been trying to do this (unsuccessfully) using the following configuration:

<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>

When it runs, the service host throws an AddressAlreadyInUseException complaining that "There is already a listener on IP endpoint 0.0.0.0:808". This actually makes sense to me because the port sharing service has opened that port in order to serve the MyService endpoint along with any other services requesting to share that port on this machine.

So it seems that the mex endpoint wants exlusive access to port 808. I can work around this by tweaking the mex endpoint like so:

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

This means that the mex endpoint now has its own exclusive port. The downside with this is that any other service which wants to expose a mex endpoint will also need a unique port for its mex endpoint. This makes it very unpredictable when looking for mex endpoints.

Is there a way to force the mex endpoint to participate in port sharing?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
Damian Powell
  • 8,655
  • 7
  • 48
  • 58

1 Answers1

9

Two options:

  1. The easy way: Change the entire binding for the mex point to netTcpBinding and have it reuse your bindingConfiguration. mexTCPBinding is only meant to be a convenience and is optional. If its not working for you, don’t use it.

  2. The hard way: You can modify the mexTCPBinding to enable sharing. The only example I’ve seen is in code here: Link

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ErnieL
  • 5,773
  • 1
  • 23
  • 27
  • Thanks for your reply. I tried changing mexTcpBinding to netTcpBinding but no dice. When I do that, the service starts up and doesn't complain about sharing problems (which is cool) but completely fails to respond to mex requests (which is uncool). Presumably there's something else I need to configure if I use netTcpBinding that I'm not aware of? – Damian Powell Nov 15 '10 at 09:42
  • @Damian did you ever solve this? Weirdly we're seeing this issue on my local machine, but not in production – James Crowley Mar 08 '12 at 15:23