Sunday 18 April 2010

Reverse Engineer a Web Service from the existing WSDL

Sometimes you need to create the web service methods and you only have the web service description file(wsdl) as a staring point. You can utilise the wsdl.exe tool to generate the web service's server stub methods. This file exists in Program Files\Microsoft SDKs\Windows\v6.0A\bin\ if you have the VS express edition installed or somewhere in the VS installation folders if you have other editions. Just by running :

wsdl.exe myservice.wsdl /l:CS /serverInterface /out:C:\myoutputdir 

the .cs file containing the interface that defines the service methods is produced. if you run wsdl.exe /? from the command line you will see what the switches actually mean.

To make use of the generated interface methods you only need to implement the generated interface in your service class:

public class Service1 : System.Web.Services.WebService, ImyserviceSoap { }

A more elegant approach would be to have the above running from inside the VS IDE. This can be achieved with use of the visual studio's external tools. Go to Tools->External Tools and add a new tool. Give it a meaningful "Title" ie. WSDL Reverse Engineering . Set the "Command" to point to the wsdl.exe ie. C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\wsdl.exe. Set the "Arguments" as follows:

$(ItemFileName)$(ItemExt) /l:CS /serverInterface /out:$(ProjectDir) 

Set the "Initial directory" as your project directory ie. $(ProjectDir). Finally, check the "Prompt for arguments" and "Use Output window" the former to have the chance to modify the command line just before running and the latter to have some feedback during the operation.

In order for the tool to run smoothly you need to have the .wsdl file included in your project. Open the .wsdl and with this file in focus go to Tools and run the external tool. Now if you want to use a .wsdl from a URL you can replace  $(ItemFileName)$(ItemExt) with the URL either on the fly when the prompt box displays or by editing the external tool.

No comments:

Post a Comment