Using ASP.NET/WebServices For UPS Shipping Quotes, Part 2By Dan Anderson
In Part 1 we looked at how to create a Web Service. In this part we'll examine how to call that Web Service via an ASP.NET page!
To call a Web Service from an ASP.NET page we need to create a proxy class DLL. This proxy class wraps all of the complex calls to a Web Service into a simple call that we can make from an ASP.NET page. Creating such a proxy class DLL is fairly simple (although a bit cryptic). It can be accomplished with a batch file:
The first line uses the
WebServiceUtil.exe utility included in the .NET SDK. Basically, it
creates a .VB file containing compiler pre-code, including references to all the required libraries and
/c:proxy command indicates that we want to create a stub or proxy class of the
web service from an SDL document. The SDL document is generated by the next portion of the command
/pa: portion is the URI path to the
web class file itself.
/l:VB portion of the line simply says that the web service language used is Visual Basic. Finally,
/n:nsUPS gives the namespace to be used to refer to the web service. The namespace is
similar to the "References" option used in Visual Basic. It declares the chunk of code functionality we wish
to employ in a page or application.
The second line of the batch file, starting with
vbc, is what actually generates the final web
service DLL. First,
vbc means Visual Basic Compiler. The next piece
defines the name of the dll we want generated, as well as the directory path to place it in. The
/t:library defines the type of output we want the compiler to generate, which, in this case, is a
DLL library. Next, the
/r: directives ensure that required support libraries are included in the
compilation process, much as we declared them via the
Imports commands in the
file itself. Finally, we define the source file to be compiled, which in this case is the
generated by the
It is generally recommended that you create a
bin directory off of your site/application's root
and place any web service dlls you create there. The PDC SDK version of ASP.NET gets a little picky and might,
in some instances, require the
Bin directory for dlls.
There, we've compiled our web class. Now, let's use it!
We won't go into the entire file, because there is too much ground to cover. Instead, we're going to concern ourselves with the portion that involves the use of our new web service. The header of the client file looks like this:
Here, we have used the
Import directive to load our new web service, using the namespace we
defined for it when we compiled it. This creates an internal reference to our new web service.
Down the page, in the
ShowRate subroutine, you will see the following:
Here, we have declared
D as a new instance of our web service, after which we call the function
in the web service (
GetPrice), passing in the required parameters. The
variable now holds the output of the web service, which is then displayed at the bottom of the page form.
Our page, when first loaded, looks like this:
After entering the required information, it calls the web service, which updates the page to look like this:
Note that underneath the "Show Rate" button we now have the price for shipping the package provided from UPS. The files for this example are included below. If you have the PDC SDK version of .NET installed, give them a try!