Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
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, tworzą i konfigurują własne elementy powiązania. W tym temacie pokazano, jak uzyskać dostęp do tych elementów powiązania i zmieniać ich właściwości, które nie są bezpośrednio widoczne w powiązaniu; w szczególności klasy BasicHttpBinding.
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:
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.
Wymagany element powiązania kodera komunikatów, albo TextMessageEncodingBindingElement lub MtomMessageEncodingBindingElement.
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ć wiązanie dostarczone przez system
Utwórz wystąpienie BasicHttpBinding klasy i ustaw tryb zabezpieczeń na poziom wiadomości.
// 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 WithUtwó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.ElementsPrzejdź przez klasę BindingElementCollection, a po znalezieniu klasy HttpTransportBindingElement, ustaw jej właściwość 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