Net.Tcp, MEX Endpoints and PortSharing in WCF

PortSharing allow to share same port on different services. In my scenario I need to expose my service metadata on a second endpoint. First, I define a baseAddress like net.tcp://localhost:9999/MyService. This allows to define an address used as root to build a dynamic URI. In our endpoints, the resulting address is: <rootAddress> + <endpointAddress>. We can start to see this sample service's configuration:

  1: <services>
  2:   <service name="MyService"               
  3:            behaviorConfiguration="MyService_ServiceBehavior">
  4:     <host>
  5:       <baseAddresses>
  6:         <add baseAddress="net.tcp://localhost:9999/MyService"/>
  7:       </baseAddresses>
  8:     </host>
  9:     <endpoint address=""                  
 10:               binding="netTcpBinding"                  
 11:               contract="IMyService"                  
 12:               bindingConfiguration="MyService_netTcpBinding"/>
 13:     <endpoint address="mex"                  
 14:               binding="netTcpBinding"                  
 15:               contract="IMetadataExchange"                  
 16:               bindingConfiguration="MyService_mexBinding"/>
 17:   </service>
 18: </services>

Both endpoints reference two netTcpBinding binding sections where I set the portSharingEnabled attribute to true:

  1: <bindings>
  2:   <netTcpBinding>
  3:     <binding name="MyService_netTcpBinding" 
  4:              maxConnections="5" 
  5:              portSharingEnabled="true">
  6:       <security mode="None">
  7:         <transport protectionlevel="None"/>
  8:       </security>
  9:     </binding>
 10:     <binding name="MyService_mexBinding" portSharingEnabled="true">
 11:       <security mode="None">
 12:         <transport protectionlevel="None"/>
 13:       </security>
 14:     </binding>
 15:   </netTcpBinding>
 16: </bindings>

To enable the publication of service metadata we must set the service behavior:

  1: <serviceBehaviors>
  2:   <behavior name="MyService_ServiceBehavior">
  3:     <serviceMetadata />
  4:   </behavior>
  5: </serviceBehaviors>

Finally, from VS2008 Command Prompt we must run, as administrator, the tool sc.exe:

C:\sc.exe config NetTcpPortSharing start= demand (the blank space between start= and demand is important!!!)

This command allow us to start the service NetTcpPortSharing when necessary. Now we can start our application...Big Smile

More info.

8 Comments

  • the default port for net.tcp is 808, so it is not necessary to provide it in the baseAddress. I think this approach is better then using some magic numbers like 9999 which could be reserved already by some service on your machine.

  • Hi Fabio! Thanks for your code, i was trying pretty much for a solution for mex over netTcp, and yours worked excellent! well explained. I just have a doubt, in the binding configuration for the service, why have you set the maxConnections="5" ? will it have any effect on scalability when hosted? I am pretty new to wcf and hence i am asking. Thanks in advance. could you reply at: aks4you(at)gmail(dot)com

  • Why i am getting this error Please advice
    "Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http]"

  • Wow! This could be one of that the most useful web sites we have ever
    come across on thesubject. Really fine post! I am also an expert in
    this topic therefore I can understand your effort.

  • First-rate stuff here, on that the other hand i noticed that several of your links are
    broken. thanks for the content anyway!

  • Once I originally commented I clicked the -Notify me when new feedback are added- checkbox and now every time a remark is added
    I learn four emails with that the same comment. Is there any other means it is easy to remove me out of
    that service? Thanks!

  • *This web site is really a walk-through for
    all of the information you wanted about this and didn’t recognize who to
    ask. Glimpse here, and you’ll definitely discover
    it.

  • It's good as your other articles : D, regards for posting .

Comments have been disabled for this content.