• Home
  • Readings
  • About
  • Disclaimer

Sankarsan’s Journal

Technology,Science,Books,Movies…..
Stay updated via RSS

  • sankarsan.wordpress.com
    61/100
  • Tags

    .NET ADO.NET AJAX ASP.NET ASP.NET 4.0 ASP.NET 4.5 ASP.NET MVC Behaviors C# C# 4.0 C++11 Channels CLR CTP 4.0 DLR Dynamic Entity Framework F# IIS IIS 6.0 IIS Hosted WCF JSON Microsoft O/R Patterns Pooling POX REST RPC Ruby RValue Scala Serialization Sessions StructureMap System.Transaction Transactions Two phase Commit Volatile RM VS 2010 WCF WCF Extensions WebService WF WF4.0
  • Archives

    • December 2012
    • August 2012
    • July 2012
    • April 2012
    • March 2012
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • June 2011
    • May 2011
    • February 2011
    • October 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • March 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
  • June 2010
    M T W T F S S
    « May   Jul »
     123456
    78910111213
    14151617181920
    21222324252627
    282930  
  • My Links

    Software Blogs - BlogCatalog Blog Directory View SANKARSAN BOSE's profile on LinkedIn Programming Blog Directory
  • Twitter Updates

    • ASP.NET 4.5 Web Forms ModelBinding – Custom Data Providers dlvr.it/2bv6j5 6 months ago
    • "@berkun: Why project managers get no respect bit.ly/VnrjPc #pm #pmi #programming"...Superb..!!! 6 months ago
    • "Tracepoint" another intetesting feature...as the execution dz nt halt..watch data an be collected..Breakpoints bcums distrbing at times. 6 months ago
    • "Analyze Solution for Clone Code" is an interesting VS 2012 feature..#KolkataGeeks #UGMeet 6 months ago
    • "@berkun: "You can't build a reputation on what you are going to do." – Henry Ford" 7 months ago

WCF & MetaData Exchange – Part 1

Posted: June 20, 2010 in .NET, C#, WCF
Tags: MetaData Exchange, WCF, WSDL
7

Web Services have to expose metadata so that clients can understand how to interact with and make use of that service endpoint.This metadata is generally exchanged in the form Web Services Description Language(WSDL) and XSD schemas describing the bindings,network addresses,messages to be exchanged & also operations and data types.WCF provides a very rich infrastructure and set of APIs to export,publish,retrieve and import service metadata.In this series of posts we will try to examine the important classes involved in this process and the extension hooks.

The WCF classes representing this metadata is as shown below:

 

md

The metadata of a service is represented the class System.ServiceModel.Description.MetdataSet which contains references to the schemas, policies,wsdl documents as a collection of System.ServiceModel.Description.MetdataSection objects.MetadataSection exposes the property MetaData (of Object type) which contains the WSDL,Policies,etc.

Exporting MetaData

The process of exporting the metadata involves converting the information related to service endpoint into understandable format like WSDL.For this abstract base class System.ServiceModel.Description.MetadataExporter is available.Presently there is one implementation available that is System.ServiceModel.Description.WsdlExporter which exports the metadata in WSDL format.The MetadataExporter class has an internal constructor hence cannot be overridden and used by custom classes.

To add any extension to the metadata export process we need to write a custom class implementing the System.ServiceModel.Description.IWsdlExportExtension interface.This class needs to be added to the WCF service infrastructure as a Service,Contract,Operation or Endpoint behavior.

The class shown below accepts a comment string as constructor parameter and adds that comment string as a documentation of the WSDL porttype element.

public class CommentExporter : Attribute,IWsdlExportExtension, IContractBehavior 
  {
      string comment; 

      public CommentExporter(string comment)
      {
          this.comment = comment;
      } 

      #region IWsdlExportExtension Members 

      void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
      {
          XmlElement elem = null;
          XmlDocument xdoc = null;
          context.WsdlPortType.Documentation = string.Empty;
          if (context.Contract != null)
          {
              if (context.WsdlPortType.DocumentationElement != null)
              {
                  elem = context.WsdlPortType.
                                              DocumentationElement.OwnerDocument.
                                                  CreateElement("wsdlportcomment");
                  elem.InnerText = comment;
                  context.WsdlPortType.DocumentationElement.AppendChild(elem);
              }
          }
      } 

      void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
      {
          return;
      } 

      #endregion 

      #region IContractBehavior Members 

      void IContractBehavior.AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
      {
          return;
      } 

      void IContractBehavior.ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
      {
          return;
      } 

      void IContractBehavior.ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)
      {
          return;
      } 

      void IContractBehavior.Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
      {
          return;
      } 

      #endregion
  } 


It also implements the IContractBehavior interface and Attribute class so that it can be applied as a contract behavior on the service contract as shown below:

[ServiceContract]
   [CommentExporter("Test Comment")]
   interface ITest
   {
       [OperationContract]
       int Add(int i,int j);
   }

Now the next step is to publish this metadata.

 Publishing MetaData

This the process of making metadata available to client over standard protocols.There are two ways of doing it.

We can expose metadata exchange endpoints compliant to WS-MetaDataExchange standards.To do this we have to use the IMetadataExchange service contract and 4 default MetadataExchange bindings e.g. mexHttpBinding,mexHttpsBinding,mexTcpBinding,mexNamedPipeBinding.A sample end point configuration is shown below:

<endpoint address=”http://localhost:7018/Test/mex” binding=”mexHttpBinding”
          bindingConfiguration=”" name=”mex” contract=”IMetadataExchange

<endpoint address="http://localhost:7018/Test/mex" binding="mexHttpBinding"
          bindingConfiguration="" name="mex" contract="IMetadataExchange">

Metadata can also be published using the ServiceMetadataBehavior class as shown below:

using(ServiceHost host = new ServiceHost(typeof(TestService)))
{
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    smb.HttpGetEnabled = true;
    smb.HttpGetUrl = new Uri("http://localhost:7018/Test/WSDL");
    host.Description.Behaviors.Add(smb);
    host.Open();
    Console.Read();
}

This means the WSDL description will be available over Http with a GET request using URL http://localhost:7018/Test/WSDL. The same can done using config as well.

On typing http://localhost:7018/Test/WSDL in the browser the WSDL will be displayed as shown below:

wsdl

In the next post we will discuss about retrieving & importing of metadata in details.

About these ads

Share this:

  • StumbleUpon
  • Digg
  • Reddit

Like this:

Like Loading...
Comments
  1. DotNetShoutout says:
    June 20, 2010 at 12:11 pm

    WCF & MetaData Exchange – Part 1 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    Reply
  2. Servefault.com says:
    June 20, 2010 at 12:12 pm

    WCF & MetaData Exchange – Part 1 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from Servefault.com…

    Reply
  3. PimpThisBlog.com says:
    June 20, 2010 at 12:14 pm

    WCF & MetaData Exchange – Part 1 « Sankarsan’s Journal…

    Thank you for submitting this cool story – Trackback from PimpThisBlog.com…

    Reply
  4. Tweets that mention WCF & MetaData Exchange – Part 1 « Sankarsan’s Journal -- Topsy.com says:
    June 20, 2010 at 4:27 pm

    [...] This post was mentioned on Twitter by techno@solidsoft, Sankarsan Bose. Sankarsan Bose said: Completed the post..WCF & MetaData Exchange – Part 1…http://bit.ly/9Nmd7b [...]

    Reply
  5. WCF Service wsDualHttpBinding and Firewall? | SEM顾问 says:
    June 21, 2010 at 6:17 am

    [...] WCF & MetaData Exchange – Part 1 « Sankarsan’s Journal [...]

    Reply
  6. How to develop a WCF service which calls another WCF service? | SEM顾问 says:
    June 21, 2010 at 6:17 am

    [...] WCF & MetaData Exchange – Part 1 « Sankarsan’s Journal [...]

    Reply
  7. sona says:
    November 16, 2010 at 6:44 pm

    Hello Sankarsan,

    I liked your article but i have one query regarding Metadata exchange using endpoints.

    while we are exchanging metadata using HTTP-GET(by using ).We can see the WSDL file on http path.But if we exposed metadata using tcp metadataexchange endpoint.How can we see the WSDL file? I tried with TCP address but not available there.

    Please help to understand this.

    Thanks in Advance

    Reply

Leave a Reply Cancel reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. ( Log Out / Change )

Twitter picture

You are commenting using your Twitter account. ( Log Out / Change )

Facebook photo

You are commenting using your Facebook account. ( Log Out / Change )

Cancel

Connecting to %s

Fileless Activation of WCF Service in .NET 4.0
Code Like A Kid using Small Basic

Blog at WordPress.com. | Theme: Greyzed by The Forge Web Creations.
Follow

Get every new post delivered to your Inbox.

Join 38 other followers

Powered by WordPress.com
%d bloggers like this: