Sdílet prostřednictvím


Postupy: Přizpůsobení vazeb poskytovaných systémem

Windows Communication Foundation (WCF) obsahuje několik systémových vazeb, které umožňují nakonfigurovat některé vlastnosti základních prvků vazby, ale ne všechny vlastnosti. Toto téma ukazuje, jak nastavit vlastnosti elementů vazby k vytvoření vlastní vazby.

Další informace o tom, jak přímo vytvářet a konfigurovat prvky vazby bez použití systémových vazeb, naleznete v tématu Vlastní vazby.

Další informace o vytváření a rozšiřování vlastních vazeb naleznete v tématu Rozšíření vazeb.

Ve službě WCF jsou všechny vazby tvořeny prvky vazby. Každý element vazby je odvozen od BindingElement třídy. Systémové vazby, jako BasicHttpBinding je vytvoření a konfigurace vlastních vazeb Toto téma ukazuje, jak získat přístup k těmto prvkům vazby a změnit jejich vlastnosti, které nejsou přímo vystaveny na vazbě; konkrétně třídy BasicHttpBinding .

Jednotlivé prvky vazby jsou obsaženy v kolekci reprezentované BindingElementCollection třídou a jsou přidány v tomto pořadí: Transaction Flow, Reliable Session, Security, Composite Duplex, One-way, Stream Security, Message Encoding a Transport. Všimněte si, že v každé vazbě nejsou vyžadovány všechny uvedené prvky vazby. Prvky vazby definované uživatelem se mohou také objevit v této kolekci elementů vazby a musí se zobrazit ve stejném pořadí jako dříve popsané. Například přenos definovaný uživatelem musí být posledním prvkem kolekce elementů vazby.

Třída BasicHttpBinding obsahuje tři prvky vazby:

  1. Volitelný prvek vazby zabezpečení, buď třída AsymmetricSecurityBindingElement použitá s přenosem HTTP (zabezpečení na úrovni zpráv), nebo TransportSecurityBindingElement třída, která se používá, když transportní vrstva poskytuje zabezpečení, v takovém případě se používá přenos HTTPS.

  2. Požadovaný prvek vazby kodéru zpráv nebo TextMessageEncodingBindingElementMtomMessageEncodingBindingElement.

  3. Požadovaný prvek transportní vazby, buď HttpTransportBindingElementnebo HttpsTransportBindingElement.

V tomto příkladu vytvoříme instanci vazby, vygenerujeme z ní vlastní vazbu , prozkoumáme prvky vazby ve vlastní vazbě a když najdeme element vazby HTTP, nastavíme jeho KeepAliveEnabled vlastnost na false. Vlastnost KeepAliveEnabled není vystavena přímo na BasicHttpBinding, takže musíme vytvořit vlastní vazbu přejít dolů na element vazby a nastavit tuto vlastnost.

Úprava systémové vazby

  1. Vytvořte instanci BasicHttpBinding třídy a nastavte její režim zabezpečení na úroveň zpráv.

    //  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. Vytvořte vlastní vazbu z vazby a vytvořte BindingElementCollection třídu z jedné z vlastností vlastní vazby.

    //  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. Projděte BindingElementCollection třídu a když najdete HttpTransportBindingElement třídu, nastavte její KeepAliveEnabled vlastnost 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
    

Viz také