Introduction

Windows Communication Foundation (WCF) is Microsoft's latest SOAP stack (and more). A SOAP stack enables programmers to work with their favorite programming model - classes - instead of manually handcrafting SOAP messages. WCF generates these classes from a WSDL, which is a metadata file each Web Service exposes with the list of operations it supports and their schema. WCF only knows to generate classes (proxy) out of WSDL 1.0 documents.

In this article, I show how to extend WCF so that it will generate classes from WSDL 2.0 documents as well.

Background

A major requirement from many Web Service is to support interoperability. This means that clients from various platforms should be able to access the Web Service. A key to that is the ability of the various platforms to generate proxies, such that instead of handcrafting SOAP, like this (highly simplified):

Isn't it nicer? A SOAP stack can generate such a class, provided that the Web Service exposes a WSDL file. This file contains all of the metadata required in order to generate the code. Here is a (tiny) sample from a WSDL:

This WSDL adheres to WSDL version 1.0, which is the prevalent version. The newer version, WSDL 2.0, is not so common. Will it ever be? Will it fulfill the promise to be the REST and SOAP services' next generation metadata? This interesting discussion is out of the scope of this article. If you are interested, read some of it in this blog.

Many SOAP stacks only support generating clients from WSDL 1.0 documents. WCF is one of them. If you try to author a WCF client to a service that uses WSDL 2.0, this can be a real pain.

In this article, I show how to consume WSDL 2.0 documents with WCF.

Using the code

Download the latest version of svcutil2.exe, either from the source attached to this article, or from the CodePlex project page.

Open the VS command console or otherwise make sure the original svcutil.exe is in the current path (usually located in C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin).

Use svcutil2 exactly like you use svcutil:

$> svcutil2.exe http://WSDL2WSDL.cloudapp.net/WSDL/simple2.WSDL

You can also use any of the svcutil known flags.

Implementation

Writing code generation for WSDL is a complex task. I chose to use a different approach: I utilized the fact that WCF already knows to generate code from WSDL 1.0 documents, so I convert WSDL 2.0 documents to WSDL 1.0 and give them to WCF to process. svcutil2 is based on the same code base as WSDL2WSDL, which is an online WSDL2 → WSDL1 converter utility.

The bits and bytes of the actual conversion are quite exhausting, but you are welcome to investigate the WSDL2WSDLConverter.cs class for the full details.

Points of interest

Some readers may ask what is the use of WSDL 2.0 anyway, if it can be reduced to a WSDL 1.0 document. The truth is that WSDL 2.0 has a lot more than WSDL 1.0, but for SOAP stacks, most of the new stuff is not relevant. Some of it is, and I had to either use creative ways to solve it or to leave it as a limitation in this version. One example is interface inheritance which is currently not supported.

I have the following WSDL from an Apache UDDI Service (juddi) that is running in a WSO2 Governance Registry application.

I hope my code snippets are included

After modifying the code to cope with some null reference exceptions and not able to handle fault types, I was able to convert the wsdl2 file and I got a file. Basically I have downloaded the wsdl2 file and converted it to a string in wsdl format not using a web server. That way I simply got the file.

I hope, you can help me. Here is the modified code for svcutil2 (I skip the fixes for the converter for now):