A Preliminary Study on WCF-6: configure a WCF Service, a preliminary study on a wcf-6 Service

Source: Internet
Author: User

A Preliminary Study on WCF-6: configure a WCF Service, a preliminary study on a wcf-6 Service

WCF Service configuration is the main part of WCF Service programming. As the basic framework of distributed development, WCF uses the configuration file method when defining services and clients that define consumption services. Although WCF also provides a hard programming method, you can configure the server and client by directly setting relevant object attributes in the Code. However, this method is not conducive to later program changes and extensions. Therefore, the configuration file brings great convenience to the WCF program programming. Through the configuration file configuration service, it can provide the flexibility of the final node and service behavior data during deployment rather than design. For this reason, the WCF framework has great scalability and customization.

Shows the structure of the WCF configuration file, which consists of three parts: services, bindings, and behaviors)

1. services Node

Each service release contains one or more endpoints ). The Service element has the following attributes:

  • Name: specifies the type of service agreement implementation. It is a fully qualified name (namespace and type name)
  • BehaviorConfiguration: Specifies the name of the service behavior element found in the behaviors element. The node in the corresponding behaviors is the serviceBehaviors node.

Endpoint specifies the binding, agreement, and address attributes of the service endpoint used to publish the service. All communication with WCF is performed through the endpoint of the service. With the endpoint, the WCF Service can access the functions provided by WCF. Common attributes are as follows:

  • Address: a string containing the endpoint address, which specifies the uniform identifier (URI) of the service element ). You can specify an absolute or relative address. If a relative address is provided, the host must provide the base address suitable for the transfer scheme used in the binding. If no address is configured, the base address is the endpoint address. The default value is an empty string.
  • Binding: Specifies how to communicate with the endpoint. It can be the binding type provided by the system, such as wsHttpBinding, WSDualHttpBinding, and NetTcpBinding. It can also be a user-defined binding type. The specified binding determines the type, security, and encoding of the transport protocol, and whether reliable sessions, transactions, or streams are supported or enabled.
  • BindingConfiguration: a string that specifies the binding name used for the end point of the instantiation. When defining an endpoint, the binding name must be in the scope. If wsHttpBinding is used, the name of bindingConfiguration corresponds to the binding of binding1 in wsHttpBinding under bindings.
  • BehaviorConfiguration: Specifies the name of the endpoint behavior element found in the behaviors element. The node in the corresponding behaviors is the endpointBehaviors node.
  • Contract: indicates the Agreement published by this endpoint. The Assembly must implement this protocol type. If the service implementation implements a single protocol type, this attribute can be omitted. The default value is an empty string.

Metadata endpoint: the WCF Service publishes metadata by publishing one or more metadata endpoints. After publishing metadata, you can use this metadata through standard protocols (such as WS-MetadataExchange (MEX) and Http/Get requests. Metadata endpoints are similar to other service endpoints. They all need the first address, a protocol, and a binding. To enable the publish metadata endpoint, you must add the ServiceMetadataBehavior service behavior to the service. By default, the WCF Service does not publish metadata endpoints. Therefore, you must add the metadata they display to the service configuration to enable metadata publishing for the service. As shown in:

 

Note: concepts related to WCF metadata

WCFWhat is service metadata?

The metadata of the WCF Service is the original description of the core service Address (Address), Binding (Communication Protocol Binding), and Contract (Service, operation, and data Contract) of the WCF Service. The metadata exposed by the service includes XSD (elements that appear in the document, attributes that appear in the document, child elements, number of child elements, sequence of child elements, null elements, elements, and attributes ). the default and fixed values of the Data Type, element, or attribute) and the WSDL document (used to describe the methods, parameters, number of parameters, order, return value, return value type, and other methods of the Service ).. Disco documentation (describes the service protocol, address, namespace, and other information ).

All these key WCF Service metadata is based on the XML language description and supports core industry standard protocols. The benefits of XSD are obvious. Based on XML, there is no special syntax. XML Schema supports a series of data types (int, float, Boolean, date, etc.) and extensible data models, supports integrated namespaces and attribute groups. These are the key parts of the formal pursuit of cross-language and cross-platform WCF distributed services.

Why expose service metadata?

After learning about the concept of WCF Service metadata, we can understand why the metadata of the service should be exposed. The metadata of the WCF Service describes the core information of the service. The client needs to understand these features to communicate with the service. To achieve communication between heterogeneous platforms or systems, the previous technology was to use Web services because of its advantages such as self-description, scalability, and platform independence. The client can obtain Service-related information based on the Web Service address, reverse serial local code, and call the Service through the Service proxy.

One of the main features of the WCF Service is cross-platform service interaction. An important reason for exposing service metadata is to solve the key issue of service interaction between heterogeneous clients. Metadata is based on XML and self-description. The client can generate local code based on the reverse sequence of the metadata of the service, whether it is c #, vb or java.

 

2. bindings Node

Configure the node attributes required for binding and bind the detailed information required to connect to the WCF Service endpoint. Therefore, the binding information must be correctly configured for each endpoint, at least the transmission mechanism (HTTP/TCP) needs to be specified for binding. You can also set other features, such as security and transaction support. The binding information may be complex or simple. Generally, the information contained in the binding to connect to the endpoint belongs to one of the following categories.

  • Protocol: Determine the security mechanism used, reliable message transmission or transaction context stream settings
  • Encoding: Determine the message encoding (such as text or binary)
  • Transmission: determine the basic transmission protocol used (such as TCP/HTTP)

3. behaviors Node

The configuration includes serviceBehaviors and endpointBehaviors.

  • ServiceBehaviors: contains the following child elements:

  

  • EndpointBehaviors: contains the following child elements:

  

 

Note: For details, refer to MSDN.

Https://msdn.microsoft.com/zh-cn/library/aa967282 (v = vs.110). aspx

Https://msdn.microsoft.com/zh-cn/library/vstudio/ms731403 (v = vs.100). aspx

 

Next, we will write an instance as usual. The instance is very simple. The project structure is shown in the right figure:

using System.ServiceModel;namespace Service{    [ServiceContract]    public interface IReqReply    {        [OperationContract]        string SayHello(string name);    }}

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service{    public class ReqReply:IReqReply    {        public string SayHello(string name)        {            return "Hello "+name;        }    }}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The App. config configuration file of the Host project is as follows:

<?xml version="1.0"?><configuration>  <system.serviceModel>    <services>      <service name="Service.ReqReply" behaviorConfiguration="ReqReplyBehavior">        

 

The Code of Program. cs is as follows:

Using System; using System. collections. generic; using System. linq; using System. text; using System. serviceModel; using Service; namespace Host {class Program {static void Main (string [] args) {using (ServiceHost ReqReplyHost = new ServiceHost (typeof (ReqReply) {ReqReplyHost. opened + = delegate {Console. writeLine ("the request response communication service has been started. Press any key to terminate it! ") ;}; ReqReplyHost. Open (); Console. Read ();}}}}

 

Reference the service http: // 127.0.0.1: 1234/ReqReply/, and name the service ReqReplyServiceRef. The client call code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Client.ReqReplyServiceRef;namespace Client{    class Program    {        static void Main(string[] args)        {            ReqReplyClient proxy = new ReqReplyClient();            Console.WriteLine(proxy.SayHello("WCF"));            Console.Read();        }    }}

 

 

                                 

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.