WCF: WSDL and Contracts

Yesterday, I talked about building a simple Windows Communication Foundation client which communicated with an ASMX web service.  In that post, I mentioned that I used Add Web Reference to create the Indigo Proxy.  That tool reads the WSDL file from your service and, among other things, creates a contract.  I thought it might be useful to describe how  to manually create a contract based on the WSDL file.  It is actually pretty straightforward and aids in the understanding of WCF.

The easiest way to create a contract is to create an interface and decorate it with the [ServiceContract] attribute.  The name of the interface can be anything you want.  The methods signatures in the interface are associated with operations in the WSDL file.  You decorate the method with the [OperationContract] attribute.  This attribute contains parameters which you use to match it to the WSDL.  For example, assume you have the following WSDL:

<wsdl:operation name=”MyAdd>

  <soap:operation soapAction=”http://tempuri.org/MyAdd style=”document />

You could use the following:

[OperationContract (Name=“MyAdd”,Action=“http://tempuri.org/MyAdd”)]

Finally, you need to set the parameter names and types, along with the return type, based on the element names in the WSDL file.  For example, assume the following element names in the WSDL file:

  <s:element name=”MyAdd>



              <s:element minOccurs=”1 maxOccurs=”1 name=”x type=”s:int />

              <s:element minOccurs=”1 maxOccurs=”1 name=”y type=”s:int />





<s:element name=”MyAddResponse>



           <s:element minOccurs=”1 maxOccurs=”1 name=”MyAddResult type=”s:int />




You can use this info to complete the contract, as follows:


public interface IMyService


[OperationContract (Name=“MyAdd”,Action=“http://tempuri.org/MyAdd”)]

int NewAdd(int x, int y);



Comments (3)

  1. Is there any chance to generate the service from the WSDL file? I know the client proxy can be generated from the WSDL but I was wondering if WCF can generate the service, as WSDL.EXE /server can do.


    Diego Gonzalez

    Lagash Systems SA

  2. Nick Kolev says:

    Yes, this is a much more interesting matter – can we generate service contract based on the WSDL file the way WSDL.exe /server does?

    The manual approach sounds like a hack, not a solution. Yes, it can be done, but it is error prone and doesn’t "scale".