WCF communication process

Source: Internet
Author: User
Tutorial 2 [communication process of a WCF application]

I. Overview

 WCF is a Web service that establishes a secure, reliable, and transactional solution across platforms ,. net remoting, Enterprise Service, WSE, MSMQ union, there is a classic comparison figure as follows:

Comparison between WCF and other distributed Technologies

II. Introduction to "A", "B", and "C" in WCF

Let's take a look at the example in our daily life. One day, the company's leaders asked you to submit a contract document, during file delivery, you can select taxi, bus, and subway transportation methods. Of course, the expenses are reimbursed based on the invoice, after arriving at the opposite Company, you need to find a manager and a receipt of the contract documents and related documents.

To complete this task, perform the following steps:

(1) first, we need to know the address of the other company and lead to "A" in WCF ".

A (address): It is an English "Address". In a computer, it is identified by a unique URI address. Through this address, we can find the WCF Service we want to call.

A solved: where to locate the WCF Service?

(2) We also need to choose our transportation mode. The results of each transportation mode are different. For example, the taxi fare is expensive, but the process is more comfortable. The time depends on the road. Bus is the cheapest and multiple lines are available. The subway is the most convenient, but sometimes it is very crowded, generally there is no seat, etc., lead to the "B" in WCF ".

B (binding): In English, it is understood as "binding, binding". Binding implements all the underlying details of communication between the client and the service. For example, what encoding is used when the client and the server transmit data? xml? Text? Binary ?... Which transmission protocol is used for transmission, TCP? HTTP? And what mechanisms are used to solve security problems, SSL? Encryption ?...

B solved: how to communicate with service?

(3) What can we do after arriving at the target company? I. Send the contract, II. get the receipt. We cannot ask the other company to give us other things, and lead to "C" in WCF ".

C (contract): What is a contract in English? Tell us what can be done, such as what cannot be done. The main function of contract is to expose all valid methods provided by a WCF Service. Contract converts each method into a corresponding message.

C solved: What functionalities do the service provide?

3. endpoint)

  WCF enables communication between applications in the network system. The communication between applications is implemented by an endpoint. In the actual example above, A, B, and C are components of the endpoint, which is the entry point for Inter-server communication calls.

4. Inter-Application Communication

In terms of items 2 and 3, we talked about a, B, c, and endpoint, And now we officially enter the communication between applications. Let's take the contract delivery process as an example:

Employee A has a note in his hand, marking: Address, binding, contract ..... the partner also has a note in his hand, marking the same content and waiting for Employee A to appear. Only when the content on the note is the same will partner a sign the contract receipt.

When we host the WCF Service, we must define one or more endpoints, and then the Serivce end processes the requests sent by the client by listening to these endpoints. Because applications rely on endpoints for communication, we must define endpoints on the client side. Only when the endpoints of the client and service fully match can we communicate.

As shown in: only a, B, c in endpointa can communicate with A, B, and C in endpointb. The same is true for endpointe and endpointd.

V. Instances

In the previous tutorial, we used IIS as the host. The client calls the WCF Service as a web application. Today, this section mainly introduces the transmission configuration in WCF. We will slightly change the content in the previous section to reflect "endpoint" and "A, B, and C ".

Because I did not hand-write any configuration code in tutorial 1, the client and Service web. config is automatically generated. When we add a service reference, IDE automatically matches the endpoint in the client configuration file with the endpoint of the referenced service. The following code is used:

Client web. config:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 3 <! -- 4 for detailed messages about how to configure ASP. NET applications, visit 5 http://go.microsoft.com/fwlink? Linkid = 169433 6 --> 7 8 <configuration> 9 <system. web> 10 <compilation DEBUG = "true" targetframework = "4.0"/> 11 </system. web> 12 13 <system. servicemodel> 14 <bindings> 15 <basichttpbinding> 16 <binding name = "basichttpbinding_iuser" closetimeout = "00:01:00" 17 opentimeout = "00:01:00" receivetimeout = "00:10:00" sendtimeout = "00:01:00" 18 allowcookies = "false" encoding = "false" encoding = "strongwildcard" 19 maxbuffersize = "65536" maxbufferpoolsize = "524288" maxcompute edmessagesize = "65536" 20 messageencoding = "text" textencoding =" UTF-8 "transfermode =" buffered "21 usedefawebwebproxy =" true "> 22 <readerquotas maxdepth =" 32 "maxstringcontentlength =" 8192 "maxarraylength =" 16384 "23 maxbytesperread =" 4096 "bytes = ""16384"/> 24 <security mode = "NONE"> 25 <transport clientcredentialtype = "NONE" proxycredentialtype = "NONE" 26 realm = ""/> 27 <message clientcredentialtype =" username "algorithmsuite =" default "/> 28 </Security> 29 </binding> 30 </basichttpbinding> 31 </bindings> 32 <client> 33 <endpoint address =" HTTP: // localhost/user. SVC "binding =" basichttpbinding "34 bindingconfiguration =" basichttpbinding_iuser "Contract =" wcfservice. iuser "35 name =" basichttpbinding_iuser "/> 36 </client> 37 </system. servicemodel> 38 </configuration>

Server Web. config code:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <configuration> 3 4 <system. web> 5 <compilation DEBUG = "true" targetframework = "4.0"/> 6 </system. web> 7 <system. servicemodel> 8 <behaviors> 9 <servicebehaviors> 10 <behavior> 11 <! -- To avoid metadata leakage, set the following value to false before deployment and delete the above metadata endpoint --> 12 <servicemetadata httpgetenabled = "true"/> 13 <! -- To receive fault exception details for debugging, set the following value to true. Set false before deployment to avoid leakage of exception information --> 14 <servicedebug includeexceptiondetailinfaults = "false"/> 15 </behavior> 16 </servicebehaviors> 17 </behaviors> 18 <servicehostingenvironment multiplesitebindingsenabled = "true"/> 19 </system. servicemodel> 20 <system. webserver> 21 <modules runallmanagedmodulesforallrequests = "true"/> 22 </system. webserver> 23 24 </configuration>

  From the two configuration files above, we found that the client system. servicemode node has the endpoint we just talked about. Why didn't the server? Isn't that against what we just mentioned? Okay, we acknowledge that the endpoint in the web. config generated by IDE is very obscure. Then let's take a look at the code of the configuration file [Delete the configuration file that looks complicated and useless for the current Learning Section] After manual modification (I put the server and client together ):

 1 <!--Service Web.config--> 2 <?xml version="1.0" encoding="utf-8"?> 3 <configuration> 4   <system.web> 5     <compilation debug="true" targetFramework="4.0" /> 6   </system.web> 7   <system.serviceModel> 8     <services> 9       <service name="WCFService">10         <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"11            contract="WCFService.IUser" />12       </service>13     </services>14   </system.serviceModel>15 </configuration>16 17 <!--Client Web.config-->18 <?xml version="1.0" encoding="utf-8"?>19 <configuration>20   <system.web>21     <compilation debug="true" targetFramework="4.0" />22   </system.web>23   <system.serviceModel>24     <client>25       <endpoint address="http://localhost/User.svc" binding="basicHttpBinding"26          contract="WCFService.IUser" />27     </client>28   </system.serviceModel>29 </configuration>

After modifying the configuration file, we performed the following operations and found that the WCF Service was still successfully executed !! This configuration file is clear enough. A, B, and C in the endpoint are the same on both the server side and the client side. That is, the endpoint exactly matches!

So why can I execute the configuration file for the first time? The answer is that we host WCF on IIS, and IIS listens to the HTTP Protocol [B determined] by default, and the address is also relative to the file address on IIS [a determined], not to mention the contract. Find the user. SVC has [C OK], so there is no need to display system on the server. servicemodel. Try again if you don't believe it. the servicemodel section is deleted. programs can run the same way! The server endpoint is determined, and the client endpoint must correspond to the server. Therefore, the endpoint in the configuration file generated by the IDE is detailed, but the server does not have an endpoint.

Vi. Summary

This tutorial describes the communication process between applications. endpoint and ABC are very important and easy to understand. Therefore, we recommend that you be as skilled as possible.

VII. Copyright

  Reprinted please indicate the source: http://www.cnblogs.com/iamlilinfeng

WCF communication process

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.