Written by 12:01 pm Programming

Generating service contract from static WSDL and XSD files

Recently I needed to generate a service contract interface for a third-party client based on their specification. My client would be hosting the service so we would be providing our own WSDL and endpoint, but the service had to conform to the end client’s requirement including which objects would be exchanged.

To make this work I needed to generate an interface and associated classes from the WSDL definition and XSDs supplied by the third-party, but at first I wasn’t sure how to turn these into a working interface that I could use in my project.

The trick to this is svcutil.exe, the ServiceModel Metadata Utility provided by Microsoft as part of the .NET Framework. Depending on what version of the Framework you’re using, this little gem will be in the following folder:

C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\

or if you’re running a 64-bit OS, replace Program Files with Program Files (x86).

All you need to do is save the WSDL and XSD files into a suitable folder, e.g. c:\Temp\, and issue the following command:

svcutil c:\Temp\*.wsdl c:\Temp\*.xsd /d:c:\Temp

The last switch /d: tells the utility where to put the generated service classes. If you leave this switch off it will try and put them in the same directory as the svcutil.exe which won’t work unless you run the command prompt ‘as an Administrator’ due to the permissions required to write inside the Program Files directory.

Once the tool has finished, you should be left with a C# class file containing your interface and associated object classes, and a config file output.config containing a default set of bindings and endpoint configurations – these will need merging into your project config when the service is ready for deployment.

(Visited 128 times, 1 visits today)
Tags: , , , Last modified: 13/07/2012
Close