15 days proficient in WCF-the last point of understanding on "binding" on the day of the week, the day of the week of the wcf

Source: Internet
Author: User

15 days proficient in WCF-the last point of understanding on "binding" on the day of the week, the day of the week of the wcf

  

In a twinkling of an eye, I had been interrupted for 10 days and did not write a blog. I was not busy with my work. I just met the Dragon Boat Festival, and recently I was fascinated by the love protection war in Tianjin... It's so nice. It's always an endless aftertaste... And

What tu Lei said was really a tmd classic, and then it was delayed. Well, let's talk about it. Let's take a look at the last thing we need to know in binding.

 

I. Channel Stack

I have mentioned the channel stack many times in my previous articles. I don't know whether you know the concept of it. In fact, it is quite simple to think about it. Since it is a stack, so there must be more than one element in this stack, right, the second one.

If it is a stack, it must follow the FILO principle. You may say that this is still very abstract. Can you give a specific example ??? Congratulations, there is actually a CreateBindingElements method in wcf,

Let's take a look at it in detail...

 

1. Let's take a look at some of the binding stacks.

  

See the monitoring window above. There are two elements in the BasicHttpBinding channel Stack: HttpTransportBindingElement and TextMessageEncodingBindingEl.

Ement, you can easily determine by name. One is "http transmission protocol" and the other is "text message encoding protocol". Then you can look at the complicated WSHttpBinding, not only does he have Basic

All the other things include the SymmetricSecurityBindingElement (Security Protocol) and TransactionFlowBindingElement (transaction stream). Now, do you have a bottom in your mind? At least I know each

For better understanding, I will draw a simplified diagram.

As shown in the figure above, I mean it. When the Client is following the WSHttpBinding protocol, the InputMessage on the Client will first go to TransactionFlow and parse ricsec.

Urity, TextMessageEncoding, and finally go to HttpTransport. Then the Service end performs "reverse processing" according to the client. after a while of disabling the token, we get the safe OutputMessage.

 

Ii. BindingElement cross-binding

If you are very careful, you will surely find that Binding is a channel stack configured by default in advance, each Binding has its own BindingElements,

Exactly these Elements can be cross-Binding, that is to say, I can freely combine Elements. Can this give us the most flexibility of these cool coders? Right, let's give a simple example,

BasicHttpBinding has two binding elements, in which TextMessageEncoding is used for soap messages, while netTcpBinding is used for BinaryMessageEncoding for soap messages,

Then you should know that I want to make a custom Binding. The message encoding is BinaryMessage and the transmission protocol is HttpTransport. How can this problem be solved ????

Host file:

1 class Program1 2 {3 static void Main (string [] args) 4 {5 ServiceHost host = new ServiceHost (typeof (HomeService), new Uri ("http: // 192.168.1.105: 1920 "); 6 7 var customBinding = new CustomBinding (); 8 9 customBinding. elements. add (new BinaryMessageEncodingBindingElement (); 10 customBinding. elements. add (new HttpTransportBindingElement (); 11 12 host. addServiceEndpoint (typeof (IHomeService), customBin Ding, "HomeServie"); 13 14 host. description. behaviors. add (new ServiceMetadataBehavior () {HttpGetEnabled = true}); 15 16 host. addServiceEndpoint (typeof (IMetadataExchange), MetadataExchangeBindings. createMexHttpBinding (), "mex"); 17 18 host. open (); 19 20 Console. writeLine ("service enabled !!! "); 21 22 Console. Read (); 23} 24}

 

Client call:

1 static void Main (string [] args) 2 {3 ServiceReference1.HomeServiceClient client = new ServiceReference1.HomeServiceClient (); 4 5 var result = client. update ("hello"); 6 7 Console. writeLine ("server value:" + result); 8 9 Console. read (); 10}

Finally, we use Fiddler to monitor it. Finally, we can see that it is garbled.

 

I hope this article will help you. In the next article, let's take a look at the Behavior in WCF, which is very interesting ~~~

 

Related Article

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.