Contract-First Web Service development

Although we normally define interface (contract) between client and server in distributed environment before we really write logic code, Visual Studio does not provide this Contract-First mechanism for Web Service application development.

What VS provides is implementation-first: Developer writes web methods first, then VS generates WSDL (contract) to allow client developer to implement web service client.

The advantage of implementation-first approach is that it is quite easy to develop a web service. Developer need not write complicated WSDL file manually. When web service client accesses the web service, .NET framework will reflect web methods in web service code and generate WSDL dynamically.

The disadvantages of implementation-first approach are:
  • Web service developer can change web method easily, which may change the generated WSDL accidentally or without enough warning. This will break the contract between web service and client.

  • Web service developer may ignore the compatibility of generated WSDL to include complex object type, like DataSet. This will make Java client difficult to get result because DataSet is only used in .NET framework, not Java framework.

So, how can we use current tools to develop web service? Actually, we can use implementation-first approach in the beginning:
  1. We can write web methods in Visual Studio without put logic code inside. Then we can let VS generate the complex WSDL for us.

  2. We examine the WSDL to make sure it does not include .NET specific data types. We may add more parameters to it.

  3. If we changed the WSDL, we can re-generate web service code using "wsdl.exe /server <WebService WSDL file>"

  4. When the contract need modification, we should explicitly update the WSDL file and re-generate web service code. We cannot modify web method code directly. This step may overwrite original code, so it is a good idea to separate implementation code into another file

If we follows the steps above, we should be able to keep interoperability between web service and client.