Wednesday, 7 May 2008

Generating C# Web Service Skeleton from wsdl

I need to create a C#.NET web service which adheres to a specific wsdl provided by a third party. Instead of browsing the wsdl content and creating the service manually I decided to look for a tool that provides a similar functionality to wsdl2java tool. What I found is wsdl.exe.

In order to generate a C# Interface for my web service I run this tool with options:
wsdl /language:CS /serverInterface wsdl_location
The command above generates the file "<wsdl _file_name>Interfaces.cs" in default location. Copy generated file to your web service project and create a new class implementing interface from generated file.

Example:
Let's assume we used the wsdl tool and have the generated interface for our service. The interface name is IMyServiceHttpBinding and contains the signatures of 2 methods: void foo(string text) and string goo(). Sample implementation may look as follows:

[WebService(Namespace = "http://somenamespace.com")]
public class MyService : IMyServiceHttpBinding
{
public void foo(string someText) {(...)}

public string goo() {(...)}
}

4 comments:

Anonymous said...

just what i was looking for! tks!

Unknown said...

¡¡¡¡ me salvaste de una semana de codificacion , gracias!!!!!! m(_)m

Gaurav said...

Hi Filip,

Need some help with creating a client for a web service. I have used your example to create the .CS file. Could you kindly guide me further.

Regards,

Gaurav

Filip Czaja said...

Gaurav

This post describes how to implement a web service itself basing on provided wsdl.

If you want to create a client that would consume the service the easiest way to achieve that is by using Visual Studio. You simply choose to add a 'Service Reference' and then you provide the service url. VS will generate client code for you.