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")))
{
    host.AddServiceEndpoint(
        typeof(NSTest.IMy_NS),
        new BasicHttpBinding(),
        "Primary");

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

    host.Description.Behaviors.Add(behavior);

    host.AddServiceEndpoint(
            typeof(IMetadataExchange),
            MetadataExchangeBindings.CreateMexHttpBinding(),
            "mex");

    host.Open();

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

<system.serviceModel>
   <services>
     <service
         name="Metadata.Samples.SimpleService"
         behaviorConfiguration="SimpleServiceBehavior">

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

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

       <endpoint address="mex"
                 binding="mexHttpBinding"
                 contract="IMetadataExchange" />
     </service>
   </services>

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

 </system.serviceModel>

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.

Share

Tags: , ,