Accessing Description / Metadata (WSDL) of WCF Web Service

WSDL or service metadata is the core for accessing or using any web service. In ASMX web service, browsing WSDL is straight forward. You develop a web service, host inside IIS and then simply browse ASMX file to view WSDL. But things are not the same with WCF service. If you try to access WCF service endpoint it says – “Metadata publishing for this service is currently disabled”. This is a common issue encountered by most of the beginners in WCF development. This results into confusion that there is something wrong with coding of WCF service while it is not true in most of the cases. So what is the reason for this message?

As a part of WCF security schema, by default no web service is allowed to share their description or Metadata unless they are configured to do so. You develop your service, test it and finally publish to production. But publishing web service to production will not enable service to be accessible unless service is configured to exchange its metadata. This is default security configuration of WCF service which blocks WSDL to be shared with users/developers.

How to make WSDL visible? This is very simple and can be achieved in following few steps.

1. WCF web service talks using end points. End point contains access address, binding and exposed contract. WCF provides out of box contract (IMetadataExchange) and binding (mexHttpBinding) to expose metadata of developed WCF web service. We just need to add this end point into service definition. Example:

 <service name="MyNamespace.MyServiceType">
    <!-- Your service end points -->
   <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
 </service>

2. WCF is a great example of decoupled extensibility. You can define services behavior separately then apply them to your services as required. Allowing metadata/WSDL exchange is one the service behavior (serviceMetadata) in WCF. We need to add this service behavior to services which further instructs or say allows services to expose their WSDL –

First Add service behavior –

 <behaviors>
     <serviceBehaviors>
         <behavior name="MyServiceTypeBehaviors" >
             <serviceMetadata httpGetEnabled="true" />
         </behavior>
     </serviceBehaviors>
 </behaviors>

If your service is exposed at https then you configure the same using “httpsGetEnabled”.

Finally enable service behavior in configured service

 <service name="MyNamespace.MyServiceType" behaviorConfiguration="MyServiceTypeBehaviors" >
  <!-- Your service end points -->
 <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
 </service>

That’s it. These simple steps enable your services to start exchanging their description. This also provides a handy way to take WSDL visibility offline as required. Thanks for reading!