Project Description Screen Project Description Screen

How to Call A Web Service From An ASP.NET Website
 

Creating a Proxy Class With Wsdl.exe

   




 

Synopsis

A developer who calls a web service from an ASP.NET website is to be able to deal with the web service using the same syntax as if the developer was interacting with a component local to the web server.  Since calling a web service involves marshalling the related parameters correctly and being able to marshal the return parameters appropriately, clearly performing such actions in an ASP.NET website would make the syntax differ from using a local component, where such explicit marshalling is not needed.

WSDL.EXE Creates A Proxy Class

To get to the end result of treating a web service call just like a call to a local component, the .NET framework contains a command-line program called wsdl.exe (Web Service Description Language), which creates a proxy class for a specific web service. This proxy class serves as an agent between the ASP.NET website and the web service.




1. A .net webpage on Web Server A (i.e. an .aspx page) instantiates an instance of the Proxy class.  For example in VB,

Dim objClass = New ProxyClassName and calls one of the Web service methods such as

objClass.MethodName(paramList)

2. The proxy class marshals the parameter list and makes an HTTP request to the Web service sitting on Web Server B, and

3. The Web service unmarshals the incoming parameters, runs the method, and marshals the output parameters. These are sent back in an HTTP response, and finally,

4: The proxy class unmarshals the return parameters and passes back the result to the ASP.NET Web page.

Now, the transaction has been completed.


The Role of the Proxy Class

Creating a Proxy Class

Creating a Proxy class involves three steps:

  1. Create the source code for the proxy class, which depends upon the WSDL of the web service.

  2. Compile the class into a DLL.

  3. Copy the DLL to the \bin directory.
     

As these three steps are completed, we can now use our Proxy class to access web service methods as if they were methods of a local component.  Visual Studio.NET can perform all of these tasks with the click of a button. These can also be accomplished via a DOS command-line screen.

When creating a proxy class, you must specify the WSDL for the web service. This XML-formatted file contains information on the incoming and outgoing parameters.

Create the proxy class by simply calling wsdl.exe with the full URL of the WSDL as the parameter.  For example,

Wsdl http://www.ExecutiveComputer.com/WSDLTest.asmx?WSDL

Creates a proxy class.

Once we have the source code we need to compile this code into a DLL. And finally, after having the Proxy class compiled into a DLL, we need to move it to the \bin directory, since that is the location where our DLLs need to reside to be picked up automatically by ASP.NET.

Using the Proxy Class

Once we have the Proxy class compiled and the DLL resides in the \bin directory, we can use it through an ASP.NET website.  For the proxy class to be generated in in C#, the wsdl.exe provides a command-line switch to specify what language to use (for generating the proxy class in that language).
A proxy class is created with the same name as the web service class. For each public Web method there is a public method in the Proxy class with the correct input and output parameters.

Note that in this Proxy class the web service is being called using the SOAP request/response payloads.
All of the complex behavior in hidden in macros or through methods defined in base classes from which our custom Math class inherits from.
Examining wsdl.exe

Wsdl.exe contains only one required parameter, a URL or path to the WSDL for the web service. The following optional parameters can be specified:

Conclusion

One would like to be able to call web services from an ASP.NET website in the same way which we call local components.  Creating a Proxy class allows us to call a web service’s methods as if the web service resided locally. The Proxy class handles the actual HTTP request as well as the marshalling of the input and output parameters.  The command-line program wsdl.exe can create the source code of the Proxy class for us based upon a web service’s WSDL. Once this class is compiled and placed in the \bin directory, it can be used through an ASP.NET website.
 

Home Site Map Contact Us Webmaster
Copyright © 1998-2007 ECC Corp. All Rights Reserved