When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Wednesday, November 22, 2000

Using ASP.NET/WebServices For UPS Shipping Quotes, Part 2

By 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!

- continued -

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:

webserviceutil /c:proxy /pa:http://localhost/asp2/ups.asmx?sdl /l:VB /n:nsUPS

vbc /out:bin\ups.dll /t:library /r:system.data.dll /r:system.web.services.dll
/r:system.net.dll /r:system.xml.serialization.dll ups.vb

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 functions. The /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:http://localhost/asp2/ups.asmx?sdl. The /pa: portion is the URI path to the web class file itself.

The /l:VB portion of the line simply says that the web service language used is Visual Basic. Finally, the /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 /out:bin\ups.dll 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 .asmx file itself. Finally, we define the source file to be compiled, which in this case is the ups.vb generated by the WebServiceUtil.exe utility.

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:

<%@ Import Namespace="nsUPS" %>

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:

Dim D As New UPS
Dim PriceData=D.GetPrice(cSvcLevel,cRateChart,cShipperZIP, _
               cReceiverZIP,"US",cPkgWeight,cResidentialInd, _

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 PriceData 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:

Web Services Screenshot

After entering the required information, it calls the web service, which updates the page to look like this:

Web Services Screenshot

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!

Happy Programming!

  • By Dan Anderson


  • Download the support code (in ZIP format)
  • Visit the ASP.NET Article Index!

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article