Skip to main content

Contract-First Web Services Development with WCF

I wanted to write this blog post to talk about my recent experience with Windows Communication Foundation (WCF) specifically using Contract-First approach with it.

With Contract-First web service development, you first design and develop your web service contract document (WSDL) and then use WCF to implement to contract instead of writing your code first and then generating the contract based on the code. Most of the examples and documentation I have seen on this subject are around creating your code first and then generating your WSDL contract. There are many benefits to using Contract-First approach over Contract-Last approach. To name a few:
  1. With Contract-Last, you can't guarantee your contract will stay the same over time since it will get generated every time you make code changes. Contract-First in a way guarantees all involved parties client and server are aware of the SAME contract.
  2. With Contract-First, you can easily create reusable schema definition. 
  3. With Contract-First, you will be able to handle versioning easier. 
There is a really good explanation and comparison of  Contact-First to Contract-Last on Spring's web site located here

With .NET 4.5, Microsoft introduced a Contract-First Tool . This tool is integrated into Visual Studio 2012 as a build task. With this tool, you can define your XML schema documents, web service operations, and your contract, and generate the data contract classes. It will not however generate your service stub for implementation of your end point operations. I also noticed this tool was only available when your project template was a "WCF Service Library" project. I needed to host the application at IIS so I wanted to use the WCF Service Application template instead of WCF Service Library and this option was not really available.

I then came across . This is an open source code generation tool that is used support Contract-First development with WCF framework. It is able to generate your data contracts as well as your service stub from your created/generated WSDL (it can generate your WSDL based on your messages XSD) within a WCF Service Application. There is some good documentation on it as well. There are few limitations to this tool though. The big one that mattered to me was that it only supports basicHttpBinding with transport security. My project requires message level security with credentials. I am having myself  modify the generated WSDL to include the security constructs as it is unable to generate it.

Overall my experience has been pretty positive with the the approach and the tooling though it may not be yet fully integrated within Visual Studio. I will post detailed examples here shortly describing how I was able to crate my XSDs, generate my WSDL and the service stub from implementation using Contract-First with