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.

Published Saturday, February 28, 2009 2:26 PM by fabioc
Filed under:

Comments

# Problem adding service reference to a WCF service hosted in a windows service - Programmers Goodies

Pingback from  Problem adding service reference to a WCF service hosted in a windows service - Programmers Goodies

# re: Net.Tcp, MEX Endpoints and PortSharing in WCF

Monday, November 28, 2011 4:44 PM by sergeyt

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.

# re: Net.Tcp, MEX Endpoints and PortSharing in WCF

Thursday, March 08, 2012 9:35 PM by Ananth

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

Leave a Comment

(required) 
(required) 
(optional)
(required)