Thursday, February 7, 2008

Consuming a Web Service in .NET

1. Get the WSDL location(http://localhost:8080/als-2.2.1/als?wsdl), make sure the XSD location in it is correct(http://localhost:8080/als-2.2.1/als?xsd=1)
2. Use wsdl.exe. In the Command Window,
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin>wsdl http://localhost:8080/als-2.2.1/als?wsdl

If you get errors:
Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Error: URI formats are not supported.

If you would like more help, please type "wsdl /?".

Simply save the .wsdl and .xsd file to the local drive and run
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin>wsdl als.wsdl

If all goes well you will see
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin>wsdl als.wsdl
Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Writing file 'C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\AL
SService.cs'.

3. Now add this file(ALSService.cs) to you current webproject and then add the Web Service to your project.
4. To refer to the the Web Service initiate an instance in WebForm1 and you can declare the DataTypes in ALSService in your Webform.

public class WebForm1 : System.Web.UI.Page
{
ALSService als = new ALSService();
..................
//calling methods
als.methodName()
...........
...........
}

Congratulations you are now ready to consume the Web Service.

Resources:
Using wsdl.exe

No comments: