如何:配置 Windows Communication Foundation 服务以使用端口共享How to: Configure a Windows Communication Foundation Service to Use Port Sharing

在 Windows Communication Foundation (WCF)应用程序中使用 net.tcp://端口共享的最简单方法是使用公开服务 NetTcpBindingThe easiest way to use net.tcp:// port sharing in your Windows Communication Foundation (WCF) application is to expose a service using the NetTcpBinding.

此绑定提供了一个 PortSharingEnabled 属性,该属性控制是否为配置了此绑定的服务启用 net.tcp:// 端口共享。This binding provides a PortSharingEnabled property that controls whether net.tcp:// port sharing is enabled for the service being configured with this binding.

下面的过程演示如何使用 NetTcpBinding 类打开一个位于统一资源标识符 (URI) net.tcp://localhost/MyService 上的终结点(首先使用代码,然后使用配置元素)。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.

使用代码在 NetTcpBinding 上启用 net.tcp:// 端口共享To enable net.tcp:// port sharing on a NetTcpBinding in code

  1. 创建一个服务来实现名为的协定 IMyService ,并调用它 MyServiceCreate a service to implement a contract called IMyService and call it MyService, .

    [ServiceContract]
    interface IMyService
    {
    
       //Define the contract operations.
    }
    
    class MyService : IMyService
    {
    
    //Implement the IMyService operations.
    }
    
    <ServiceContract()> _
    Friend Interface IMyService
    
        'Define the contract operations.
    
    End Interface
    
    Friend Class MyService
        Implements IMyService
    
        'Implement the IMyService operations.
    
    End Class
    
  2. 创建 NetTcpBinding 类的一个实例,并将 PortSharingEnabled 属性设置为 trueCreate an instance of the NetTcpBinding class and set the PortSharingEnabled property to true.

    NetTcpBinding portsharingBinding = new NetTcpBinding();
    portsharingBinding.PortSharingEnabled = true;
    
    Dim portsharingBinding As New NetTcpBinding()
    portsharingBinding.PortSharingEnabled = True
    
  3. 创建一个 ServiceHost,并在其中为 MyService 添加一个服务终结点,该终结点使用启用了端口共享的 NetTcpBinding 并在终结点地址 URI“net.tcp://localhost/MyService”上进行侦听。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".

    ServiceHost host = new ServiceHost( typeof( MyService ) );
    host.AddServiceEndpoint( typeof( IMyService ), portsharingBinding,"net.tcp://localhost/MyService" );
    
    
    Dim host As New ServiceHost(GetType(MyService))
    host.AddServiceEndpoint(GetType(IMyService), portsharingBinding, "net.tcp://localhost/MyService")
    

    备注

    此示例使用默认的 TCP 端口 808,因为终结点地址 URI 未指定其他端口号。This example uses the default TCP port of 808 because the endpoint address URI does not specify a different port number. 由于在传输绑定上显式启用了端口共享,因此该服务可以与其他进程中的其他服务共享端口 808。Because port sharing is explicitly enabled on the transport binding, this service can share port 808 with other services in other processes. 如果不允许使用端口共享并且其他应用程序已在使用端口 808,则该服务在打开时会引发 AddressAlreadyInUseExceptionIf port sharing were not allowed and another application were already using port 808, this service would throw an AddressAlreadyInUseException when it was opened.

使用配置在 NetTcpBinding 上启用 net.tcp:// 端口共享To enable net.tcp:// port sharing on a NetTcpBinding in configuration

  1. 下面的示例演示如何使用配置元素来启用端口共享以及添加服务终结点。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>  

另请参阅See also