Udostępnij za pośrednictwem


Instrukcje: Konfigurowanie usługi Windows Communication Foundation na potrzeby współużytkowania portów

Najprostszym sposobem korzystania z udostępniania portów net.tcp:// w aplikacji Windows Communication Foundation (WCF) jest uwidocznienie usługi przy użyciu .NetTcpBinding

To powiązanie zapewnia PortSharingEnabled właściwość, która określa, czy udostępnianie portów net.tcp:// jest włączone dla usługi skonfigurowanej za pomocą tego powiązania.

Poniższa procedura przedstawia sposób używania NetTcpBinding klasy do otwierania punktu końcowego w identyfikatorze URI (Uniform Resource Identifier) net.tcp://localhost/MyService, najpierw w kodzie, a następnie przy użyciu elementów konfiguracji.

Aby włączyć udostępnianie portów net.tcp:// w kodzie netTcpBinding

  1. Utwórz usługę w celu zaimplementowania kontraktu o nazwie IMyService i wywołania go 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. Utwórz wystąpienie NetTcpBinding klasy i ustaw PortSharingEnabled właściwość na true.

    NetTcpBinding portsharingBinding = new NetTcpBinding();
    portsharingBinding.PortSharingEnabled = true;
    
    Dim portsharingBinding As New NetTcpBinding()
    portsharingBinding.PortSharingEnabled = True
    
  3. ServiceHost Utwórz i dodaj do niego punkt końcowy usługi, MyService który używa włączonego NetTcpBinding udostępniania portów i nasłuchuje identyfikatora URI adresu punktu końcowego "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")
    

    Uwaga

    W tym przykładzie użyto domyślnego portu TCP 808, ponieważ identyfikator URI adresu punktu końcowego nie określa innego numeru portu. Ponieważ udostępnianie portów jest jawnie włączone w powiązaniu transportu, ta usługa może udostępniać port 808 innym usługom w innych procesach. Jeśli udostępnianie portów nie było dozwolone, a inna aplikacja korzystała już z portu 808, ta usługa zgłosi AddressAlreadyInUseException błąd po otwarciu.

Aby włączyć udostępnianie portów net.tcp:// w konfiguracji netTcpBinding

  1. W poniższym przykładzie pokazano, jak włączyć udostępnianie portów i dodać punkt końcowy usługi przy użyciu elementów konfiguracji.
<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>  

Zobacz też