Sdílet prostřednictvím


Jak na to: Přizpůsobení vazby System-Provided

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 WCF jsou všechny vazby složeny z vazebních prvků. Každý element vazby je odvozen z BindingElement třídy. Systémové vazby, jako je BasicHttpBinding, vytvářejí a konfigurují vlastní prvky vazby. Toto téma ukazuje, jak získat přístup k těmto prvkům vazeb a změnit jejich vlastnosti, které nejsou na vazbě přímo vystaveny; konkrétně třída BasicHttpBinding.

Jednotlivé prvky vazby jsou obsaženy v kolekci reprezentované třídou BindingElementCollection a jsou přidány v tomto pořadí: Transaction Flow, Reliable Session, Zabezpečení, 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 třída TransportSecurityBindingElement, 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, TextMessageEncodingBindingElement nebo MtomMessageEncodingBindingElement.

  3. Požadovaný prvek vazby přenosu, buď HttpTransportBindingElement, nebo HttpsTransportBindingElement.

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

Upravit systémovou vazbu

  1. Vytvořte instanci třídy BasicHttpBinding 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 třídu BindingElementCollection 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 třídu BindingElementCollection a když najdete třídu HttpTransportBindingElement, nastavte její vlastnost KeepAliveEnabled 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 = {httpElement.KeepAliveEnabled}");
            httpElement.KeepAliveEnabled = false;
            Console.WriteLine($"\tAfter:  HttpTransportBindingElement.KeepAliveEnabled = {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é