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, .
<ServiceContract()> _ Friend Interface IMyService 'Define the contract operations. End Interface Friend Class MyService Implements IMyService 'Implement the IMyService operations. End Class
[ServiceContract] interface IMyService { //Define the contract operations. } class MyService : IMyService { //Implement the IMyService operations. }
Create an instance of the NetTcpBinding class and set the PortSharingEnabled property to true.
Dim portsharingBinding As New NetTcpBinding() portsharingBinding.PortSharingEnabled = True
NetTcpBinding portsharingBinding = new NetTcpBinding(); portsharingBinding.PortSharingEnabled = true;
Create a ServiceHost and add the service endpoint to it for MyService that uses the port sharing-enabled NetTcpBinding and that listens at the endpoint address URI "net.tcp://localhost/MyService".
Dim host As New ServiceHost(GetType(MyService)) host.AddServiceEndpoint(GetType(IMyService), portsharingBinding, "net.tcp://localhost/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" />
<services>
<service name="MyService">
<endpoint address="net.tcp://localhost/MyService"
binding="netTcpBinding"
contract="IMyService"
bindingConfiguration="portSharingBinding" />
</service>
</services>
</system.serviceModel>
See Also
Tasks
How to: Enable the Net.TCP Port Sharing Service