wc-binding model (i)

Source: Internet
Author: User

First, the use of BasicHttpBinding to achieve message communication

The WCF infrastructure consists of the service model layer and the channel layer, and binding is a two-tier direct link. The binding creates a channel stack for processing messages, which enables the transmission and processing of messages. In the binding model, there are many communication objects, channel listeners, channel factories, etc., which are in a linked list, and each layer of the list is a single-linked list to maintain its own message processing relationship and channel relationship. When a channel listener is created, the list of listener layers is triggered, and different channel managers based on different channels are created together, and these channel managers create their own channels.

Less nonsense, on the code, on the server side:

classProgram {Static voidMain (string[] args) {BasicHttpBinding bind=NewBasicHttpBinding (); Ichannellistener<IReplyChannel> listener = bind. Buildchannellistener<ireplychannel> (NewUri ("http://localhost:8866")); Listener.            Open (); IReplyChannel Replychannel=Listener.            Acceptchannel (TimeSpan.MaxValue);            Replychannel.open ();  while(true) {RequestContext context=replychannel.receiverequest (TimeSpan.MaxValue); Message msg=context.               Requestmessage;               Console.WriteLine (msg); Context.            Reply (CreateMessage (BIND)); }        }        Private StaticMessage createmessage (Binding bind) {XNamespace ns="http://www.cnblogs.com/lh218"; XElement Body=NewXElement (NewXElement (ns +"Response",NewXElement (ns +"result",3))); returnMessage.createmessage (bind. MessageVersion,"http://www.cnblogs.com", body); }    }

BasicHttpBinding first creates a channel listener that creates a channel after open and accepts the client's message in the while loop. A TimeSpan type parameter is passed in the Receiverequest method to indicate the wait time interval. Network communication takes time, and if you wait for a long time without receiving a message to throw an exception, this parameter defines the time range, where the maximum value in the pass indicates an infinite wait. The RequestContext object is returned after the message is accepted, and it can be used to get the message and reply to the message.
Client:

 classProgram {Static voidMain (string[] args) {BasicHttpBinding bind=NewBasicHttpBinding (); Ichannelfactory<IRequestChannel> request = Bind. Buildchannelfactory<irequestchannel>(); Request.            Open (); IRequestChannel Requestchannel=request. CreateChannel (NewEndpointAddress ("http://localhost:8866"));            Requestchannel.open (); Message msg=requestchannel.request (CreateMessage (BIND));        Console.WriteLine (msg); }        Private StaticMessage createmessage (Binding bind) {XNamespace ns="http://www.cnblogs.com/lh218"; XElement Body=NewXElement (NewXElement (ns +"Request",NewXElement (ns +"x",1),NewXElement (ns +"x",2))); returnMessage.createmessage (bind. MessageVersion,"http://www.cnblogs.com", body); }    }

Create a channel factory, open, create a channel, open, create a channel, you can send a message directly. This defines a static method createmessage is used to create a message, from the content of the message can be easily seen that the client sends the request to know how much 1+2 equals, the server reply equals 3. Of course, these messages are created by yourself, and when you actually invoke a method that implements the contract, these messages are created automatically within WCF.

Messages sent by the client:

<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <to s:mustunderstand="1"xmlns="http://schemas.microsoft.com/ws/2005/05/addrEssing/none">http://localhost:8866/</To><action s:mustunderstand="1"xmlns="http://schemas.microsoft.com/ws/2005/05/Addressing/none">http://www.cnblogs.com</Action></s:Header> <s:Body> <request xmlns="http://www.cnblogs.com/lh218"> <x>1</x> <x>2</x> </Request> </s:Body></s:Envelope>

Service-side reply message:

<s:envelope xmlns:s= "http://schemas.xmlsoap.org/soap/envelope/" >
<s:header/>
<s:Body>
<response xmlns= "http://www.cnblogs.com/lh218" >
<result>3</result>
</Response>
</s:Body>
</s:Envelope>

wc-binding model (i)

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.