WCF .Net WSDL Generation and Consumption

One of the semi-hidden features of WCF found both as a consumer and a producer of webservices is the industry standard Web Services Description Language (WSDL) which can be consumed and exposed by the tools and code of a .Net web service. This article talks about the Tribal Knowledge that optionally allows one to expose a WSDL as a publisher and can consume a foreign WSDL to be used by code by a generated proxy. To access a wsdl simply ad a ?wsdl to the address and see if it comes back as xml such as http://localhost:8000/Omega?wsdl.

Expose a WSDL using MetaDataExchange (MEX)

The ability to expose a WSDL is not a default when creating a webservice. One has two options to expose the WSDL. The first is programmatically in code such as this when creating the service host

using (ServiceHost host = new ServiceHost(typeof(NSTest.HelloNS),
   new Uri("http://localhost:8000/Omega")))
        new BasicHttpBinding(),

    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
    behavior.HttpGetEnabled = true;




The other way is to place similar information in the config file, either web.config or app.config depending on how it is being hosted.  This xml is copied from How to: Publish Metadata for a Service Using a Configuration File


           <add baseAddress="http://localhost:8001/MetadataSample"/>

       <endpoint address=""
                 contract="Metadata.Samples.ISimpleService" />

       <endpoint address="mex"
                 contract="IMetadataExchange" />

       <behavior name="SimpleServiceBehavior">
         <serviceMetadata httpGetEnabled="True" policyVersion="Policy12" />
         <serviceDebug includeExceptionDetailInFaults="False" />


Consume a WSDL to create a Proxy

If a foreign webservice has exposed a WSDL, one can consume that webservice methods by a generated proxy. Again there are two ways to generate that proxy. Both actually use the same tool. That tool is Svcutil.exe which does its magic on the command line or one can browse for it in VS2008 by using the Add Service Reference command. That command simply encapsulates the SVCUtil.exe to generate a proxy class from the exposed wsdl. To see more on SVCUtil.exe see Retrieving Metadata.


One Comment

  1. Mohammad says:

    I had hoped to see an example of how to consume and use wsdl service using wcf.

Leave a Reply