WCF learning tour-WCF Service configuration (14th), and wcf learning Tour Service

Source: Internet
Author: User
Tags msmq

WCF learning tour-WCF Service configuration (14th), and wcf learning Tour Service
I. Overview

  We talked about boarding in the previous chapter and used the configuration file in the previous example. This article mainly describes how to apply the configuration file to improve the flexibility of the WCF program. When compiling a WCF Service application, it is also a major task to write configuration items. In the previous examples, I used configuration files to simplify the code. By publishing endpoints, WCF discloses services to clients, including service addresses, transmission and message encoding of services used to send and receive messages, and security types required by services. After we write these configuration items into the configuration file, we can modify some variable information of WCF without compilation, which improves the flexibility of the program.


1) if the configuration is written in the Code, the configuration items in the configuration file will not work.

2) The Web program is configured in Web. config, and the application is configured in App. config.

 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:

WCFComparison with other distributed Technologies

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

Let's take a look at an example of our daily life. One day, O2O received an order for the home nail service, the transportation modes that can be selected during the Meijia on-site service are "electric vehicles", "public transit", and "Subway". When you arrive at the customer's home, you need to find the customer. After the service is completed, A customer's service evaluation and payment confirmation are required.

To complete this task, perform the following steps:

(1) first, we need to know the customer's address and extract "A" from 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.

(2) The Meijia Division also needs to select the appropriate transportation mode. The results of each transportation mode are different. For example, if an electric vehicle needs to own its own vehicle, the time depends on the road conditions. Public transit is the cheapest, and multiple routes can be selected, with poor time controllability. 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 server. For example, how the Message transmitted between the client and the server is encoded -- text/XML, binary, and MTOM; what kind of Transport -- TCP, Http, Named Pipe, and MSMQ is used for Message transmission; and the mechanism used to solve the Secure Messaging problem-SSL and Message Level Security.

(3) What can we do after arriving at the customer's home? I. Give the customer a manicure, II. Get the Service Evaluation and payment confirmation. We cannot ask the customer to give other things to introduce "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. In terms of Message Exchange, Contract converts each operation to the corresponding Message Exchange Pattern -- MEP (Request/Response; One-way; Duplex)

4) Behavior: the main function of Behavior is to customize some necessary Behavior of the Endpoint during runtime. For example, when the server calls back the client, it times out; the client uses the Credential type; and whether Transaction is supported.


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 process of home manicure as an example:


There is an order in Meijia's hand, marking: Address, binding, contract ..... the customer also has an order in his hand, marking the same content, and has to wait for the appearance of Meijia. Only when the content on the order is the same, the Meijia division will serve the customer. After the customer completes the service, it will perform service evaluation and payment confirmation.

When we host the WCF Service, we must define one or more endpoints, and then the server listens to these endpoints to process requests sent from the client. Because applications rely on endpoints for communication, the end node must be defined on the client. Only when the client and the end point of the server completely match can the communication be performed.


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

1) Service configuration

Under the configuration node in the configuration file in. config, there are: <system. serviceModel> </system. serviceModel> nodes, which mainly have three levels. The following code is used:


<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <system. serviceModel> <! -- Configure service and endpoint start --> <services> <service> <endpoint> </service> </services> <! -- Configure the service and end point --> <! -- Start binding configuration --> <bindings> <netTcpBinding> <binding> </netTcpBinding> </bindings> <! -- End of configuration binding --> <! -- Start behavior configuration --> <behaviors> <serviceBehaviors> <behavior> </serviceBehaviors> </behaviors> <! -- End of configuration behavior --> </system. serviceModel> </configuration>



  Service configuration section [required]: Configure services, interfaces, and endpoints. Each Service has the following two attributes. Name: namespace. class name [service implementation class]. BehaviorConfiguration: Name found in the behaviors node.

Binding configuration section [Optional]:Configure binding, such as http and tcp.

  Behavior configuration section [Optional]: Configure behaviors, such as authentication.

2) Instance


<? Xml version = "1.0"?> <Configuration> <system. web> <compilation debug = "true"/> </system. web> <! -- When deploying the service library project, the content of the config file must be added to the host's app. config file. system. configuration does not support config files for libraries. --> <system. serviceModel> <protocolMapping> <add scheme = "http" binding = "basicHttpBinding"/> <add scheme = "net. tcp "binding =" netTcpBinding "/> <add scheme =" net. pipe "binding =" netNamedPipeBinding "/> <add scheme =" net. Msmq "binding =" netMsmqBinding "/> </protocolMapping> <! -- Service --> <services> <! -- Name: namespace. type name --> <! -- BehaviorConfiguration: behavior name. For details, see the name of the behavior configuration section --> <service behaviorConfiguration = "simpleBehavior" name = "WCFService. WCFService"> <! -- Unless fully qualified, the address will be related to the base address provided above. Each Service can have multiple endpoints --> <! -- Address: Specifies the external URI of this Endpoint. This URI can be an absolute Address or a relative Address relative to baseAddress. If this attribute is blank, the Endpoint address is baseAddress --> <! -- BindingConfiguration: binding name. See the name of the binding configuration section --> <endpoint name = "endpointService" address =" http://localhost:8000/ WCFService "binding =" basicHttpBinding "contract =" WCFService. IWCFService "> </endpoint> <! -- This endpoint does not use secure binding. You should ensure security or delete it before deployment. --> <! -- <Endpoint address = "mex" binding = "mexHttpBinding" contract = "IMetadataExchange"/> --> 



The result is as follows:


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.