WCF-Basic Knowledge

Source: Internet
Author: User

WCF-Basic Knowledge

(1) Introduction to the metadata exchange method of the WCF Service:

There are two solutions for the WCF Service to publish its own metadata. One is the provision of metadata based on the HTTP-GET protocol; the other is the exchange of metadata for the Mex endpoint, using a dedicated
Is called the Mex metadata exchange endpoint.
The metadataexchangeclientmode Enumeration type is defined in the system. servicemodel. Description namespace. The Code is as follows:

using System;namespace System.ServiceModel.Description{// Summary:// Specifies the exchange mode used to obtain metadata.public enum MetadataExchangeClientMode{// Summary:// A WS-Transfer Get request is used.MetadataExchange = 0,//// Summary:// An HTTP GET request is used.HttpGet = 1,}}

The metadata exchange endpoint is similar to other endpoints, including its own address, binding, and contract (Service, operation, and data contract ), however, the service contract used is
The interface imetadataexchange provided by WCF. The two methods for publishing metadata use two different standard network transmission protocols. The former is an HTTP/GET request, and the latter is WS-metadataexchange (Mex:
The basic binding protocols supported by WCF include HTTP, https, TCP, and IPC.).
After the metadata exchange service is enabled, you must explicitly configure the metadata exchange behavior. Next we will introduce the implementation process of the two methods of metadata exchange configuration and programming in detail.

(2) How to Implement the metadata exchange configuration of the WCF Service:

 

[2.1] configure the HTTP-GET metadata exchange method:
You need to configure the service behavior and base address. The client can view the service metadata based on the base address.. The Code is as follows:

<service name="WcfServiceApp.WCFService" behaviorConfiguration="WcfServiceApp.WCFServiceBehavior">

3. Detailed description of the implementation process of the metadata exchange programming of the WCF Service:

[3.1] WCF Service metadata exchange HTTP-GET programming implementation:
You must add a reference to the namespace, using system. servicemodel. description; we define the class and interface information for the service metadata operation in this namespace, the specific implementation of the HTTP-GET
The Code is as follows:

Servicemetadatabehavior metadatabehavior; // defines the service behavior variable, metadatabehavior = host. description. behaviors. find <servicemetadatabehavior> (); // obtain the behavior list of the host if (metadatabehavior = NULL) // if there is no behavior for the Service's original data exchange, instantiate and add the original service data exchange behavior {metadatabehavior = new servicemetadatabehavior (); Uri httpaddress = new uri ("http: // localhost: 8001/"); metadatabehavior. httpgeturl = httpaddress; metadatabehavior. httpgetenabled = true; // set the HTTP host. description. behaviors. add (metadatabehavior );}

The first step is to obtain the list of service behaviors. If no configuration is set, we instantiate the original data exchange behavior of the service and set the http mode
. Host. description. behaviors. Add (metadatabehavior); Add the behavior of the host service.

View in the browser, for example, the suffix is WSDL.

 

[3.2] WCF Service metadata exchange WS-* programming implementation:
The metadata exchange code of HTTP, TCP, and IPC is implemented respectively. The method is slightly different from the http-Get method. We need to instantiate our own binding element and binding, and finally pass it as a parameter to the host.
Host instance. The specific implementation code is as follows:
// 2 implement ws * original data exchange through programming

// Bindingelement tcpbindingelement = new tcptransportbindingelement (); bindingelement httpbindingelement = new httpstransportbindingelement (); bindingelement pipebindingelement = new vertex (); // instantiate the instance binding tcpbinding = new custombinding (tcpbindingelement); binding httpbinding = new custombinding (httpbindingelement); binding pipebinding = new custombinding (pipebindingelement ); // URI tcpbaseaddress = new uri ("net. TCP: // localhost: 9001/"); Uri httpbaseaddress = new uri (" http: // localhost: 9002/"); Uri pipebaseaddress = new uri (" net. pipe: // localhost/"); host. addserviceendpoint (typeof (wcfservice. iwcfservice), new nettcpbinding (), tcpbaseaddress); host. addserviceendpoint (typeof (wcfservice. iwcfservice), new wshttpbinding (), httpbaseaddress); host. addserviceendpoint (typeof (wcfservice. iwcfservice), new netnamedpipebinding (), pipebaseaddress); // servicemetadatabehavior metadatabehavior; // defines the service behavior variable, metadatabehavior = host. description. behaviors. find <servicemetadatabehavior> (); // obtain the behavior list of the host if (metadatabehavior = NULL) // if there is no behavior for the Service's original data exchange, instantiate and add the original service data exchange behavior {metadatabehavior = new servicemetadatabehavior (); host. description. behaviors. add (metadatabehavior);} // if no Mex node is available, you can use the code to judge and add the Mex node host. addserviceendpoint (typeof (imetadataexchange), tcpbinding, "mex"); host. addserviceendpoint (typeof (imetadataexchange), httpbinding, "mex"); host. addserviceendpoint (typeof (imetadataexchange), pipebinding, "mex ");

In the browser, check that the suffix is changed to Mex.

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.