WCF Technical Analysis 28: Get the metadata for yourself

Source: Internet
Author: User

The method of metadata publishing determines the acquisition behavior of metadata, and the WCF Service metadata architecture system realizes metadata publishing based on Ws-mex and http-get through ServiceMetadataBehavior, and the realization of metadata acquisition is different for these two kinds of protocols. We first implement the metadata acquisition method based on Ws-mex.

This article supporting source code: http://www.bianceng.net/dotnet/201212/655.htm

I. Metadata acquisition based on Ws-mex

ServiceMetadataBehavior the release of Ws-mex based metadata by creating MEX endpoints, an introduction to the article series on how to publish a service as WSDL we know that metadata publishing can actually be viewed as hosting a metadata service on the server side , we get the metadata in the form of a service invocation.

Since MEX endpoints are not intrinsically different from endpoints in general, we only need to create a matching endpoint of the service metadata publisher, which sends the desired request message and obtains metadata information by replying to the message. Now, take our familiar computing services for example, add a MEX endpoint to the service at the time of the service boarding, using the MEX bindings and addresses, respectively: mexHttpBinding and Http://127.0.0.1:9999/calculatorservice/mex.

1: <?xml version= "1.0" encoding= "Utf-8"?>
2: <configuration>
3: <system.serviceModel>
4: <services>
5: <service name= "Artech.MetataRetrieval.Services.CalculatorService" behaviorconfiguration= "Mexbehavior" >
6: <endpoint address= "Http://127.0.0.1:9999/calculatorservice" binding= "ws2007httpbinding" contract= "artech.me" TataRetrieval.Services.ICalculator "/>
7: <endpoint address= "Http://127.0.0.1:9999/calculatorservice/mex" binding= "mexhttpbinding" contract= "Imetadata" Exchange "/>
8: </service>
9: </services>
: <behaviors>
One: <serviceBehaviors>
<behavior name= "Mexbehavior" >
<servicemetadata httpgetenabled= "true" Httpgeturl= "Http://127.0.0.1:3721/calculatorservice/metadata"/&gt ;
: </behavior>
: </serviceBehaviors>
: </behaviors>
: </system.serviceModel>
: </configuration>

The following code shows the program that the client obtains the metadata, which is not the same as the General Service invocation. First by specifying the appropriate binding (metadataexchangebindings.createmexhttpbinding ()) and address (the destination address of the metadata: http://127.0.0.1:9999/ Calculatorservice/mex) to create the Channelfactory<tchannel> object (because the MEX endpoint contract type is IMetadataExchange, The TChannel type here is IMetadataExchange). Because the input and output parameters of the Get method for the MEX endpoint contract IMetadataExchange are message objects, we create the Message object and specify the action that matches the Ws-mex. The service invocation is then passed in to the service proxy created through channelfactory<tchannel>. Finally, the Metadataset object containing the metadata is extracted from the reply message and written to an XML file.

1:using System;
2:using System.Diagnostics;
3:using System.ServiceModel;
4:using System.ServiceModel.Channels;
5:using System.ServiceModel.Description;
6:using System.Text;
7:using System.Xml;
8:namespace Artech.metataretrieval
9: {
10:class Program
11: {
12:static void Main (string[] args)
13: {
14:metadataset metadata = null;
15:using (channelfactory<imetadataexchange> channelfactory = new Channelfactory<imetadataexchange&gt ;(metadataexchangebindings.createmexhttpbinding (), New EndpointAddress ("Http://127.0.0.1:9999/calculatorservice/mex"))
16: {
17:imetadataexchange proxy = Channelfactory.createchannel ();
18:using (proxy as IDisposable)
19: {
20:message request = Message.createmessage (Messageversion.default, "http://schemas.xmlsoap.org/ws/200 4/09/transfer/get ");
21:metadata = proxy. Get (Request). Getbody<metadataset> ();
22:}
23:}
24:using (XmlWriter writer = new XmlTextWriter ("Metadata.xml", Encoding.UTF8))
25: {
26:metadata. WriteTo (writer);
27:}
28:process.start ("Metadata.xml");
29:}
30:}
31:}

When the program executes successfully, the XML file containing the metadata will be output via IE (assuming IE is the default XML launcher), Figure 1 is the screenshot after the run.

Figure 1 using IE to display the obtained metadata (published in Ws-mex mode)

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.