Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

WCF Support for WSDL 2.0

5.00/5 (1 vote)
12 Feb 2011CPOL3 min read 44.6K   457  
A command line utility to generate WCF proxies from WSDL 2.0 documents.

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):

XML
<Envelope>
   <Body>
 
      <AddUser>
         <Name>Yaron</Name>
         <Blog>http://webservices20.blogspot.com/</Blog>
         <Books>
             <Book>
                 <Name>My First book</Name>
             </Book>

            ...
         </Books>
    </AddUser>

  </Body> 
</Envelope>

the developer can work with classes like this:

C#
public class User 
{
    string Name;
    Uri Blog;
    Book[] Books;
}

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:

XML
<s:element name="EchoString">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" 
                      name="s" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
...

<WSDL:message name="EchoStringSoapIn">
    <WSDL:part name="parameters" element="tns:EchoString" />
  </WSDL:message>

...

<WSDL:operation name="EchoString">
      <WSDL:input message="tns:EchoStringSoapIn" />
      <WSDL:output message="tns:EchoStringSoapOut" />
</WSDL:operation>

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

  1. Download the latest version of svcutil2.exe, either from the source attached to this article, or from the CodePlex project page.
  2. 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).
  3. 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.

Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)