WCF Getting Started Tutorial series two

Source: Internet
Author: User

I. Overview

 WCF is able to build a cross-platform secure, trustworthy, transactional solution that is a webservice,.net remoting,enterprise service,wse,msmq, with a classic contrast chart as follows:

Comparison of WCF and other distributed technologies table

Ii. Introduction to "A", "B", "C" in WCF

We first look at an example of life, one day, the company's leadership let you send a contract document, the process of sending documents you can choose the mode of transportation for "taxi", "bus", "Subway", of course, the cost is based on the invoice to reimburse, to the other company after you have to find a manager, and a copy of receipt of the Contract Documents and related documents.

To accomplish this task we perform the following major steps:

(1) We first need to know the address of the other company, which leads to "A" in WCF.

A (address): The English language is understood as "addresses", in the computer is identified by a URI unique address, through which we can find the WCF service we want to invoke.

A resolved: Where to locate the WCF Service?

(2) We also have to choose our mode of transportation, each way of transportation to achieve different results. such as: Taxi cost more expensive, but the process comfortable, time depends on the road conditions. Buses are the cheapest, and multiple lines can be selected. The subway is the most convenient, but occasionally very crowded, generally no seat, etc., lead to the "B" in WCF.

B (binding): English is understood as "bundle, bind," and the binding implements all the underlying details of the client and service communication. Such as: What kind of encoding do we use when we transfer to the client and server, XML? Text? Binary? ... What kind of transport protocol is used for transmission, TCP? Http? And what mechanisms are used to address security issues, SSL? Encryption? ...

B solved: How to communicate with service?

(3) What can we do after we get to the other company? I. Send the contract, II. take the receipt. We can't ask the other company to give us something else that leads to the "C" in WCF.

C (contract): English is understood as "contract", what is the contract? Tell us what we can do and what we can't do. The primary role of contract is to expose all the valid methods provided by a WCF service. Contract actually transforms each method into a corresponding message.

C Solved: What functionalities does the Service provide?

Third, Endpoint (end point)

  WCF implements the communication of the various applications of the network system. The communication for each application is implemented as an "endpoint (Endpoint)". A, B, and C are part of the endpoint in the actual example above, and he is the portal for communication calls between servers.

Iv. Communication between applications

In the second and third items, we talked about a, B, C, and endpoint, and now formally enter the communication between applications. Let's take the example of the process of sending a contract just now:

Employee A has a note in his hand, marked with: address, binding, contract ..... The partner also has a note that marks the same content and has been waiting for employee a to appear. Partner A will sign a contract receipt only if the content on the note is the same.

When we host a WCF service, we have to define one or more endpoints, and then the serivce side handles requests from the client by listening to those endpoints. Since applications are communicated by endpoint, we must also define endpoints on the client side to communicate only when the client exactly matches the endpoint of the service.

As shown: Only the Endpointa in a, B, C and ENDPOINTB in A, B, c exactly match to communicate. Endpointe and Endpointd are the same.

V. Examples

In the previous section of the tutorial we used IIS as the host, and the client invoked the WCF service as a Web application. Today this section mainly introduces the configuration of transport in WCF, we change the contents of the previous section slightly to reflect the "Endpoint" and "A, B, C".

Since I did not write any configured code in the tutorial, the client and the service side of Web. config are automatically generated, and when we add a service reference, the IDE automatically matches the endpoint in the client's configuration file with the endpoint of the referenced service. As shown in the following code:

Client Web. config:

1 <?xml version="1.0"Encoding="Utf-8"?>23 <!--4For more information about how to configure an ASP. NET application, go to5 http://go.microsoft.com/fwlink/?linkid=1694336--78 <configuration>9 <system.web>Ten <compilation debug="True"Targetframework="4.0"/></system.web>12<system.serviceModel><bindings><basicHttpBinding><binding name="Basichttpbinding_iuser"Closetimeout="00:01:00"opentimeout="00:01:00"Receivetimeout="00:10:00"Sendtimeout="00:01:00"allowcookies="False"Bypassproxyonlocal="False"Hostnamecomparisonmode="StrongWildcard"Maxbuffersize="65536"Maxbufferpoolsize="524288"Maxreceivedmessagesize="65536"messageencoding="Text"Textencoding="Utf-8"Transfermode="Buffered"usedefaultwebproxy="True"><readerquotas maxdepth="32"Maxstringcontentlength="8192"Maxarraylength="16384"Maxbytesperread="4096"Maxnametablecharcount="16384"/><security mode="None"><transport clientcredentialtype="None"Proxycredentialtype="None"realm=""/><message clientcredentialtype="UserName"Algorithmsuite="Default"/></security></binding></basicHttpBinding></bindings><client><endpoint address= "http://localhost/user.svc  "Binding="  Basichttpbinding "34 Bindingconfiguration= "basichttpbinding_iuser" Contract= "wcfservice.iuser"  Name=basichttpbinding_iuser  "/>36 </client> 37 </system.servicemodel>38 </configuration                 

Service-side web. config code:

1 <?xml version="1.0"Encoding="Utf-8"?>2 <configuration>34 <system.web>5 <compilation debug="True"Targetframework="4.0"/>6 </system.web>7 <system.serviceModel>8 <behaviors>9 <serviceBehaviors>Ten <behavior><!--to avoid leaking metadata information, set the following values before deployment toFalse and delete the above metadata endpoint--<servicemetadata httpgetenabled="True"/><!--to receive the fault exception details for debugging, set the following values toTrue Before deployment, set toFalse to avoid leaking exception information--<servicedebug includeexceptiondetailinfaults="false15 </ Behavior>16 </servicebehaviors>17 </ Behaviors>18 <servicehostingenvironment Multiplesitebindingsenabled= "true"/>19 </ System.servicemodel>20 <system.webserver> <modules runallmanagedmodulesforallrequests= " "/>22 </system.webserver>23 24 </configuration>        

  From the above two configuration files we found that the client System.servicemode node has the endpoint we just talked about, and the server why not? Isn't this a violation of what we just said? Well, we admit that endpoint in the IDE-generated Web. config is very cryptic. So let's look at the code for the configuration file (I put the server and the client together) after the manual modification [of the configuration section that looks very complex and is useless for the current learning]:

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">Ten <endpoint address="Http://localhost/User.svc"Binding="BasicHttpBinding"contract="Wcfservice.iuser"/></service></services></system.serviceModel></configuration>16<!--Client web.config--><?xml version="1.0"Encoding="Utf-8"?><configuration><system.web><compilation debug="True"Targetframework="4.022 </ 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 perform the discovery that the WCF service still executes successfully!! This time the configuration file is clear enough, whether on the server or on the client side, endpoint A, B, C are exactly the same. That's exactly the end point!

So why does the first configuration file work? The answer is that we host WCF on IIS, and IIS is listening by default to the HTTP protocol [b OK] and the address is relative to the file address on IIS [a OK], not to mention the contract, to find user.svc everything has [C determined], So on the service side there is no need to display the write System.ServiceModel, do not believe you try, the server configuration file in the System.ServiceModel section deleted, the program can be run! Server-side endpoint determined that the client's endpoint nature and the service side to correspond, so the IDE in the configuration file generated client endpoint write very detailed, and the service side is not endpoint.

Vi. Summary

This tutorial mainly describes the communication process between applications, endpoint and ABC are very important, but also better understanding, so it is recommended to be as proficient as possible.

Vii. Copyright

  Reprint Please specify source:Http://www.cnblogs.com/iamlilinfeng

Getting Started with WCF Tutorial series two

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.