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...
More info.