Friday, June 3, 2011

Wcf Wsdl Generation and Including Schemas

For some reason, when you browse to the WSDL for a ServiceContract you do not get included Schema in the WSDL Types.

For example: Here's a basic demo Wcf Service.
 Notice the link to view the actual WSDL file for this service.

This is what the WSDL file looks like :
 Notice how the <wsdl:Types /> element is empty.  Ideally a WSDL document should be the one stop shop for describing the service.

To view the schemas of the data contracts and other types used by the service you need to add other parameters to the URL as follows:

Xsd0 shows the service actions, the methods and their parameters.

Xsd1 shows the standard scalar types used, ie strings, arrays etc.

Xsd2 shows the custom data contract types defined and used in the service.

Xsd3 (not shown above) will show any enumeration definitions that you have included in operation contracts.

All these schemas can be copied into the <wsdl:types /> element or imported into the element as follows:


<xsd:schema targetNamespace="">
      <xsd:import schemaLocation="http://localhost:55401/CalculatorService.svc?xsd=xsd0" namespace="" />
      <xsd:import schemaLocation="http://localhost:55401/CalculatorService.svc?xsd=xsd1" namespace="" />
      <xsd:import schemaLocation="http://localhost:55401/CalculatorService.svc?xsd=xsd2" namespace="" />

As an alternative to using a browser to explore the WSDL for  a service you can use the SVCUTIL to extract the WSDL and XSD and save them locally.

SVCUTIL /t:metadata http://url_to_service_here.

This will extract the WSDL and all related XSD files onto your local disk in the current folder.

As a courtesy to any third party you are engaging with and co-developing a service always ensure you give them schema's in the WSDL.


  1. Hi Ben,
    great article. I've have implemented a lot of asmx web services and now I'm moving to WCF.
    Is it possible to set the wsdl / xsd constraints and restrinctions using the DataMember/DataContract attributes?

  2. Simple answer is no (not that I know of). I have heard of some people editing the resulting XSD schema's inside the WSDL. But this will put you in a dangerous situation when you want to make service changes and forget to update the WSDL. I would recommend using a IParameterInspector to inspect individual incoming operation arguments and validate them. You can invent some of your own attributes to describe how to validate them (ie Regex or similar) then look for the presence of those attributes inside your implementation of IParameterInspector. HTH

  3. WCF and .Net 4.5 has the option to create a single WSDL file out of the box with no effort or third party libraries. Finally!

  4. Here's another technique worth considering:

    It provides you with a custom service host (or factory).