WCF Metadata Exchange

Source: Internet
Author: User

1. What is 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 purpose of exposing metadata is very clear. The client needs to understand these features to communicate with the WCF Service.

2. metadata publishing

1. http_get-based Metadata

By default, WCF does not publish metadata, but you can configure it in the configuration file to publish metadata:

    <behaviors>      <serviceBehaviors>        <behavior name="commonService">          <serviceMetadata httpGetEnabled="true"/>        </behavior>      </serviceBehaviors>    </behaviors>

<service name="PciService.ImagePathService" behaviorConfiguration="commonService">

By default, the HTTP base address of the service must be registered to use the http_get client address. If the host does not configure the HTTP base address load service, an exception is thrown.

If http_get-based Metadata Exchange is enabled, enter the configured address in the browser to access public metadata.

Enable metadata exchange programmatically:

            using(ServiceHost host = new ServiceHost(typeof(ImagePathService)))            {                ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();                if (metadataBehavior == null)                {                    metadataBehavior = new ServiceMetadataBehavior();                    metadataBehavior.HttpGetEnabled = true;                    host.Description.Behaviors.Add(metadataBehavior);                }                host.Open();

The effects of publishing metadata programmatically and using configuration files are the same. You can also view public metadata in the browser.

1. Add a behavior to the behavior set. This behavior set is maintained by the host for the service type. The description attribute is defined in servicehostbase.

//// Summary: // obtain the description of the service to be hosted. //// Return result: // The system. servicemodel. description. servicedescription of the service to be carried. Public servicedescription description {Get ;}

This description describes all aspects of the service. In servicedescription, the behaviors attribute is defined:

// Abstract: // obtain the behavior associated with the service. //// Return result: // system. Collections. Generic. keyedbytypecollection <titem> that contains the system. servicemodel. description. iservicebehavior Service Association Behavior type. Public keyedbytypecollection <iservicebehavior> behaviors {Get ;}
Public class keyedbytypecollection <titem>: keyedcollection <type, titem> {// Abstract: // return the first entry with the specified type in the set. //// Type parameter: // T: // type of the item to be searched in the set. //// Return result: // if it is of the reference type, the object of the type T is returned. If it is of the value type, the value of the type T is returned. If the collection does not contain objects of the type T, the default value of the returned type is: if it is a reference type, the default value is // NULL; if it is a value type, the default value is 0. [Targetedpatchingoptout ("performance critical to inline this type of method defined SS ngen image boundaries")] public t find <t> ();}

Defense programming is used in the host code. First, the Code calls the find in the keyedbytypecollection <t> method to determine whether the Mex endpoint is provided in the configuration file. If null is returned, it indicates that the configuration file does not contain metadata.

2. Metadata Exchange endpoint

WCF Metadata Exchange

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.