WCF Technology Profiling 27: How to publish a service as WSDL

Source: Internet
Author: User
Tags reflection static class wsdl

[Based on WS-MEX implementation] (provides an emulator)

Through the introduction of how to publish a service into wsdl[programming article] We know how to apply servicemetadatabehavior such a service form to the corresponding service by programming or configuring. Thus, a metadata publishing mechanism based on Http-get or Ws-mex is implemented. So what is the specific implementation principle within WCF? I believe a lot of people are curious about this, and the content of this article will revolve around this topic.

First, from the WCF distribution system

If the reader wants to have a comprehensive and in-depth understanding of the implementation principle of the metadata publishing mechanism within WCF, it must have a clear understanding of the WCF service-side distribution system. Here we first give a general introduction to the distribution system. WCF's entire distribution system is built for service boarding (Hosting), and the basic structure of the system can basically be illustrated by figure 1.

Figure 1 WCF service-side distribution system

When we create a ServiceHost object and successfully host a service, WCF creates one or more ChannelDispatcher objects for the ServiceHost object based on the different listening addresses. Each channeldispatcher has its own channellistener, and these channellistener are bound to the corresponding listening address to listen for requests from outside. For each Channellistener object, there is a match with one or more EndpointDispatcher objects, each endpointdispatcher corresponding to an endpoint.

For each endpointdispatcher, a runtime, or DispatchRuntime, is created for the initialization. DispatchRuntime has a series of Run-time objects that handle requests, activation objects, and execution methods, where we focus primarily on an object called Instancecontextprovider. Instancecontextprovider is used to provide InstanceContext objects that encapsulate a corresponding service instance.

Second, based on the Ws-mex mode of metadata publishing is how to achieve it?

Now let's move on to metadata publishing, first to talk about metadata publishing methods based on Ws-mex protocol. In this metadata publishing mode, the service end publishes metadata through the MEX endpoint, and the client creates the corresponding MEX endpoint to obtain the metadata, which is not fundamentally different from the service invocation in the general sense. You can think of metadata acquisition as a service that provides metadata.

If we add a MEX endpoint to a service programmatically or in a configured way, WCF creates a channeldispatcher for the service after it has been successfully hosted. The ChannelDispatcher has a channellistener for listening to metadata requests, the address to listen to, and the address of the metadata publication. EndpointDispatcher objects that are based on the MEX endpoint are also created and associated with the ChannelDispatcher. The associated DispatchRuntime is also created when the EndpointDispatcher is initialized. Like the dispatchruntime associated with a normal endpoint, DispatchRuntime based on the MEX endpoint also has the same set of Run-time objects. However, since none of the services really used to provide metadata are hosted, The DispatchRuntime instancecontextprovider (default is Persessioninstancecontextprovider) is not getting the InstanceContext object that contains a true service instance.

So, if you can customize the DispatchRuntime Instancecontextprovider so that it can provide a instancecontext normally, and that InstanceContext contains a service instance that really can provide metadata, and the service class class implements the contract interface IMetadataExchange of the MEX endpoint, then all the problems are solved. In fact, that's what ServiceMetadataBehavior is doing, and the type of service that is used to provide metadata is a internal type defined within WCF: Wsmeximpl.

   1:internal class Wsmeximpl:imetadataexchange
2: {
3: //other Members
4: Public IAsyncResult beginget (message request, AsyncCallback callback, object state);
5: Public message endget (IAsyncResult result);
6: Private Metadataset Gathermetadata (string dialect, string identifier);
7: Public message get (message request);
8:}

When the ServiceMetadataBehavior ApplyDispatchBehavior method is executed, ServiceMetadataBehavior creates the Wsmeximpl object, The InstanceContext object is created and used as the singletoninstancecontext of the MEX endpoint DispatchRuntime. Then create a singletoninstancecontextprovider as the instancecontextprovider of the DispatchRuntime. The dispatchruntime of the MEX endpoint can then use its instancecontextprovider to provide instancecontext that encapsulates the Wsmeximpl instance.

Although these contents of the appeal are not responsible, but require the reader to have a clear understanding of the WCF instance context mechanism, the reader who is unfamiliar with it can parameter the 9th chapter of WCF Technical Profiling (Volume 1). In order to deepen the reader's understanding of the Ws-mex metadata publishing mechanism, I will make a simple example demo.

Three, example demo: Simulation ServiceMetadataBehavior implementation based on Ws-mex metadata Publishing

Next, I will be completely based on the principle of servicemetadatabehavior, the principle described above, to create a custom service behavior for Ws-mex metadata Publishing, and Source code downloads from here. First, we'll write some auxiliary code. Because in this example I need to create some Run-time objects related to DispatchRuntime, and many objects are not exposed (many are internal types, such as Singletoninstancecontextprovider), I need to create them through the mechanism of reflection. In addition, we need to assign values to some private or internal properties of some objects, as well as using reflection, so I wrote the following two helper methods:

1:using System;
2:using System.Globalization;
3:using System.Reflection;
4:namespace Servicemetadatabehaviorsimulator
5: {
6:public Static Class Utility
7: {
8:public static T createinstance<t> (String typeqname, Type[]parametertypes, object[] parameters) where T:CLA Ss
9: {
10:type Type = Type.GetType (typeqname);
11:bindingflags BindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | bindingflags.static;
12:constructorinfo ConstructorInfo = type. GetConstructor (BindingFlags, Type.defaultbinder, parametertypes, NULL);
13:return activator.createinstance (Type, BindingFlags, Type.defaultbinder, parameters, CULTUREINFO.INVARIANTCU Lture) as T;
14:}
15:
16:public static void setPropertyValue (object target, String propertyname, Object PropertyValue)
17: {
18:bindingflags BindingFlags = BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance;
19:propertyinfo PropertyInfo = target. GetType (). GetProperty (PropertyName, BindingFlags);
20:propertyinfo.setvalue (target, propertyvalue, NULL);
21:}
22:}
23:}

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.