WCF Fundamentals: Binding (III)

Source: Internet
Author: User

In the WCF binding architecture, bindings create binding elements, binding elements create binding listeners/binding factories, binding listeners/binding factories to create channels.

The binding in WCF is a channel stack with multiple channels connected, which must contain the transmission channel and the encoded channel, and the transmission channel must be at the bottom of the stack, in the channel stack can add a custom channel or some protocol channel (WS-* standard), in these protocol channels can some message processing, such as transaction flow, to ensure the safe transmission of messages and so on. In the channel stack of WCF, each channel has this single processing function, and the channel stack composed of multiple channels can handle the message of many functions.

Binding

 Public Abstract classbinding:idefaultcommunicationtimeouts{ Public VirtualIchannellistener<tchannel> buildchannellistener<tchannel> (bindingparametercollection parameters)whereTChannel:class, IChannel; Public Abstractbindingelementcollection createbindingelements (); Public Virtual BOOLCanbuildchannellistener<tchannel> (bindingparametercollection parameters)whereTChannel:class, IChannel;}

Create a channel listener in the binding class by calling the Buildchannellistener<tchannel> method; The Createbindingelements () method creates a collection of binding elements because the binding element creates a channel listener, so the binding collection also forms a list of channels;

In WCF's system bindings, nettcpbinding, which supports TCP/IP, implements the Createbindingelements method internally:

 Public classnettcpbinding:binding{Private voidInitialize () { This. Transport =Newtcptransportbindingelement ();  This. Encoding =Newbinarymessageencodingbindingelement ();  This. Context =getdefaulttransactionflowbindingelement ();  This. session =Newreliablesessionbindingelement ();  This. reliablesession =NewOptionalreliablesession ( This. session); }

private static Transactionflowbindingelement getdefaulttransactionflowbindingelement ()
{
return new Transactionflowbindingelement (false);
}

 Public Overridebindingelementcollection createbindingelements () { This.        Checksettings (); Bindingelementcollection Elements=Newbindingelementcollection (); Elements. ADD ( This. Context); if( This. reliablesession.enabled) {elements. ADD ( This. session); } securitybindingelement Item= This.         Createmessagesecurity (); if(Item! =NULL) {elements.         ADD (item); } elements. ADD ( This. Encoding); BindingElement Element2= This.         Createtransportsecurity (); if(Element2! =NULL) {elements.         ADD (Element2); }          This. Transport. Extendedprotectionpolicy = This. Security.         Transport.extendedprotectionpolicy; Elements. ADD ( This. Transport); returnelements.    Clone (); }}

The Createbindingelements method in NetTcpBinding

1. Create a binding element collection (Bindingelementcollection) and add the binding elements (Transactionflowbindingelement) that are supported for the bound transaction flow in the collection;

2. If the current binding enables reliable session transfer, add the binding elements (Reliablesessionbindingelement) that support the send and receive channels required to generate reliable sessions between endpoints in the binding collection;

3. Generate a binding element (SecurityBindingElement) that supports channel SOAP message security based on the security settings of the bindings added to the binding element collection;

4. Add the required message encoding binding element to the collection of binding elements, because TCP uses binary transport, and therefore creates an encoded binding element (Binarymessageencodingbindingelement) that supports binary encoding;

5. If the security setting for the binding is to use secure transport (for example, HTTPS) to provide security (securitymode.transport/ securitymode.transportwithmessagecredential) or create a binding element (windowsstreamsecuritybindingelement, which guarantees transmission assurance and authentication), SslStreamSecurityBindingElement)

6. At the end of the binding element collection, add a transport binding element (Tcptransportbindingelement) that supports transport, guaranteeing the transmission channel at the bottom of the stack;

The Createbindingelements method creates a collection of binding elements for a binding, which is created by the set of binding elements when the channel listener or channel factory is created;

 Public Abstract classbinding:idefaultcommunicationtimeouts{

Public VirtualIchannellistener<tchannel> buildchannellistener<tchannel> (Uri listenuribaseaddress,stringListenurirelativeaddress, Listenurimode listenurimode, bindingparametercollection parameters)whereTChannel:class, IChannel { This. Ensureinvariants (); BindingContext Context=NewBindingContext (NewCustomBinding ( This), parameters, Listenuribaseaddress, listenurirelativeaddress, Listenurimode); Ichannellistener<TChannel> Listener = context. Buildinnerchannellistener<tchannel>(); Context. Validatebindingelementsconsumed (); This. Validatesecuritycapabilities (listener. Getproperty<isecuritycapabilities>(), parameters); returnListener; }}

In the Buildchannellistener method of the binding class, the Ensureinvariants method is called first, in fact the internal call Createbindingelements collection, The binding context (BindingContext) object is then created, and the channel listener is created through the Buildinnerchannellistener method of the binding context;

BindingContext

 Public classbindingcontext{Privatecustombinding binding; Privatebindingparametercollection bindingparameters; PrivateUri listenuribaseaddress; PrivateSystem.ServiceModel.Description.ListenUriMode Listenurimode; Privatebindingelementcollection remainingbindingelements;  PublicIchannellistener<tchannel> buildinnerchannellistener<tchannel> ()whereTChannel:class, IChannel {return  This. Removenextelement (). Buildchannellistener<tchannel> ( This); }     Privatebindingelement removenextelement () {bindingelement element= This.remainingbindingelements.remove<bindingelement>(); if(element = =NULL)        {         ThrowDiagnosticUtility.ExceptionUtility.ThrowHelperError (NewInvalidOperationException (System.ServiceModel.SR.GetString ("nochannelbuilderavailable",New Object[] { This. Binding. Name, This. Binding.         Namespace})); }            returnelement; }}

In the binding above class, CustomBinding represents the binding object that created the binding context, and bindingelementcollection represents the context collection of the binding element of the bound object;

When calling Buildinnerchannellistener to create a channel listener, first remove a binding element from the bindingelementcollection element, and removes the binding element from the Bindingelementcollection collection, calling BindingElement's Buildchannellistener method to create the binding monitor, The Buildinnerchannellistener method of the binding context is called when BindingElement executes the Buildchannellistener method. Since the Bindingelementcollection collection is dynamic, the first element of the collection is created each time, and the first binding element is removed, and when all the binding elements have been called, a channel stack is created that consists of the binding elements in the order of collection;

WCF Fundamentals: Binding (III)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.