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.
Create a service to implement a contract called
IMyServiceand call it
ServiceHost host = new ServiceHost( typeof( MyService ) ); host.AddServiceEndpoint( typeof( IMyService ), portsharingBinding,"net.tcp://localhost/MyService" );
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.
- 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>