Instrukcje: dostosowywanie powiązania udostępnionego przez system

Program Windows Communication Foundation (WCF) zawiera kilka powiązań dostarczanych przez system, które umożliwiają skonfigurowanie niektórych właściwości podstawowych elementów powiązania, ale nie wszystkich właściwości. W tym temacie pokazano, jak ustawić właściwości elementów powiązania w celu utworzenia powiązania niestandardowego.

Aby uzyskać więcej informacji na temat bezpośredniego tworzenia i konfigurowania elementów powiązania bez używania powiązań dostarczanych przez system, zobacz Powiązania niestandardowe.

Aby uzyskać więcej informacji na temat tworzenia i rozszerzania powiązań niestandardowych, zobacz Rozszerzanie powiązań.

W programie WCF wszystkie powiązania składają się z elementów powiązania. Każdy element powiązania pochodzi z BindingElement klasy . Powiązania dostarczane przez system, takie jak BasicHttpBinding tworzenie i konfigurowanie własnych elementów powiązania. W tym temacie przedstawiono sposób uzyskiwania dostępu do tych elementów powiązania i zmieniania ich właściwości, które nie są bezpośrednio widoczne w powiązaniu; w szczególności, BasicHttpBinding klasa.

Poszczególne elementy powiązania są zawarte w kolekcji reprezentowanej przez BindingElementCollection klasę i są dodawane w następującej kolejności: Przepływ transakcji, Reliable Session, Security, Composite Duplex, One-way, Stream Security, Kodowanie komunikatów i Transport. Należy pamiętać, że nie wszystkie wymienione elementy powiązania są wymagane w każdym powiązaniu. Elementy powiązania zdefiniowane przez użytkownika mogą być również wyświetlane w tej kolekcji elementów powiązania i muszą być wyświetlane w tej samej kolejności, co wcześniej opisane. Na przykład transport zdefiniowany przez użytkownika musi być ostatnim elementem kolekcji elementów powiązania.

Klasa BasicHttpBinding zawiera trzy elementy powiązania:

  1. Opcjonalny element powiązania zabezpieczeń , AsymmetricSecurityBindingElement klasa używana z transportem HTTP (zabezpieczenia na poziomie komunikatu) lub TransportSecurityBindingElement klasa, która jest używana, gdy warstwa transportu zapewnia zabezpieczenia, w tym przypadku używany jest transport HTTPS.

  2. Wymagany element powiązania kodera komunikatów lub TextMessageEncodingBindingElementMtomMessageEncodingBindingElement.

  3. Wymagany element powiązania transportu , HttpTransportBindingElementlub HttpsTransportBindingElement.

W tym przykładzie utworzymy wystąpienie powiązania, wygenerujemy z niego powiązanie niestandardowe, zbadamy elementy powiązania w powiązaniu niestandardowym, a gdy znajdziemy element powiązania HTTP, ustawimy jego KeepAliveEnabled właściwość na false. Właściwość KeepAliveEnabled nie jest uwidoczniona bezpośrednio w obiekcie BasicHttpBinding, dlatego musimy utworzyć powiązanie niestandardowe, aby przejść do elementu powiązania i ustawić tę właściwość.

Aby zmodyfikować powiązanie dostarczone przez system

  1. Utwórz wystąpienie BasicHttpBinding klasy i ustaw tryb zabezpieczeń na poziom komunikatów.

    //  Create an instance of the T:System.ServiceModel.BasicHttpBinding
    //  class and set its security mode to message-level security.
    BasicHttpBinding binding = new BasicHttpBinding();
    binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;
    binding.Security.Mode = BasicHttpSecurityMode.Message;
    
    '  Create an instance of the T:System.ServiceModel.BasicHttpBinding 
    '  class and set its security mode to message-level security.
    Dim binding As New BasicHttpBinding()
    With binding.Security
        .Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate
        .Mode = BasicHttpSecurityMode.Message
    End With
    
  2. Utwórz powiązanie niestandardowe na podstawie powiązania i utwórz klasę BindingElementCollection na podstawie jednej z właściwości powiązania niestandardowego.

    //  Create a custom binding from the binding
    CustomBinding cb = new CustomBinding(binding);
    //  Get the BindingElementCollection from this custom binding
    BindingElementCollection bec = cb.Elements();
    
    '  Create a custom binding from the binding 
    Dim cb As New CustomBinding(binding)
    '  Get the BindingElementCollection from this custom binding 
    Dim bec = cb.Elements
    
  3. Pętla przez klasę, a po znalezieniu HttpTransportBindingElementBindingElementCollection klasy ustaw jej KeepAliveEnabled właściwość na false.

    //  Loop through the collection, and when you find the HTTP binding element
    //  set its KeepAliveEnabled property to false
    foreach (BindingElement be in bec)
    {
        Type thisType = be.GetType();
        Console.WriteLine(thisType);
        if (be is HttpTransportBindingElement)
        {
            HttpTransportBindingElement httpElement = (HttpTransportBindingElement)be;
            Console.WriteLine("\tBefore: HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled);
            httpElement.KeepAliveEnabled = false;
            Console.WriteLine("\tAfter:  HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled);
        }
    }
    
    '  Loop through the collection, and when you find the HTTP binding element
    '  set its KeepAliveEnabled property to false
    For Each be In bec
        Dim thisType = be.GetType()
        Console.WriteLine(thisType)
        If TypeOf be Is HttpTransportBindingElement Then
            Dim httpElement As HttpTransportBindingElement = CType(be, HttpTransportBindingElement)
            Console.WriteLine(Constants.vbTab & "Before: HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled)
            httpElement.KeepAliveEnabled = False
            Console.WriteLine(vbTab & "After:  HttpTransportBindingElement.KeepAliveEnabled = {0}", httpElement.KeepAliveEnabled)
        End If
    Next be
    

Zobacz też