How to: Configure a Windows Communication Foundation Service to Use Port Sharing
The easiest way to use net.tcp:// port sharing in your Windows Communication Foundation (WCF) application is to expose a service using the NetTcpBinding.
This binding provides a PortSharingEnabled property that controls whether net.tcp:// port sharing is enabled for the service being configured with this binding.
The following procedure shows how to use the NetTcpBinding class to open an endpoint at the Uniform Resource Identifier (URI) net.tcp://localhost/MyService, first in code and then by using configuration elements.
To enable net.tcp:// port sharing on a NetTcpBinding in code
Create a service to implement a contract called IMyService and call it MyService, .
ServiceHost host = new ServiceHost( typeof( MyService ) ); host.AddServiceEndpoint( typeof( IMyService ), portsharingBinding,"net.tcp://localhost/MyService" );
Note: This example uses the default TCP port of 808 because the endpoint address URI does not specify a different port number. Because port sharing is explicitly enabled on the transport binding, this service can share port 808 with other services in other processes. If port sharing were not allowed and another application were already using port 808, this service would throw an AddressAlreadyInUseException when it was opened.
To enable net.tcp:// port sharing on a NetTcpBinding in configuration
The following example shows how to enable port sharing and add the service endpoint using configuration elements.
<system.serviceModel> <bindings> <netTcpBinding name="portSharingBinding" portSharingEnabled="true" /> </bindings> <services> <service name="MyService"> <endpoint address="net.tcp://localhost/MyService" binding="netTcpBinding" contract="IMyService" bindingConfiguration="portSharingBinding" /> </service> </services> </system.serviceModel>