Integrating ASP.NET XML Web Services with 'Classic' ASP ApplicationsBy Paul Bunting
Microsoft is investing millions in promoting .NET and its advantages. One of the many plugged advantages of the Microsoft .NET initiative is XML Web Services – the ability to remove your business logic from custom DLLs and COM/DCOM components (helping to avoid DLL hell) and hosting your middle tier business logic as Web Services, which can be accessed by all your applications (Web Sites, Distributed Applications, etc.) (see this article for more information on creating and consuming Web Services in .NET). But how do you use some of these new Web Services you have developed in a staggered upgrade of your existing 'Classic' ASP Applications?
This article intends to illustrate how to consume Web Services written with .NET through a classic ASP page. By placing an application's business logic in a Web Service abstraction layer, and by being able to consume Web Services through classic ASP pages, one can have their existing ASP Web applications share the same business logic as ASP.NET applications on the same Web server. This can be especially helpful if an existing ASP application works fine and you do not have the time to fully port it to ASP.NET, but you still wish it to be able to work in close synchrony with that new ASP.NET application you're programming.
The example for this article is based on a real-world need: when conducting business to business eCommerce you sometimes need to calculate special discounts for regular customers in order to secure their business. In this article we will create a Web Service that will calculate the special discount allocated to a business. We will finish by integrating this Web Service into a simple classic ASP application!
For this article I will be using the Northwinds database available in Microsoft Access. I had to make a small change to the Northwinds database
Products table's structure:
specifically I added a
CostPrice field of type money (which I then populated with some
random amounts). (The altered Northwinds database is available in the
The Web Service
As discussed in Creating and Consuming a Web Service, Web Services can be created with any text editor, such as the Web Matrix Project, Visual Studio .NET, or others. In this article I will be examining how to create a Web Service using Visual Studio .NET.
To start, create a new ASP.NET Web Project from within Visual Studio.NET.
Next, delete the default Web form it adds and then add a new Web Service
eCommerce.asmx - the Web Service itself being named
Within the Web Service file change the default namespace to
ClassicASP_nDotNET.eCommerce and then add the Web method
The Web Method
CalculateDiscountCompanyX is a simple function that will
be used to calculate the business discount for CompanyX. (Clearly a more robust solution would be to
create a more generic method, like
CompanyID uniquely identified a company. Then, a database lookup could be performed
to determine the discount(s) the company enjoys, if any. However, this article focuses on integrating
.NET Web Services with existing classic ASP application.)
For our application, let's assume that there is a minimum discount for CompanyX (set at 10%), as well as a safety net of cost +7.5% as a minimum profit for our business. Both the company's discount and our desired profit are hard coded into the Web method (again, not very robust, but we're focusing on integration here).
CalculateDiscountCompanyX Web method is fairly simple, taking the following input
UnitPrice- the recommended retail price (RRP) (type
CostPrice- the actual cost of the product (i.e., how much our company had to pay to get the product) (type
AccessCode- a password for security of web service (type
Upon error the
CalculateDiscountCompanyX Web method returns a negative value. The source code
for the method can be seen below:
Integrating your .NET Web Service into Existing Classic ASP Applications
To make it a lot easier to get your classic ASP pages to talk to .NET Web Services you should download and install the MS SOAP Toolkit 2.0. This will allow you to talk to your new web services using SOAP – which is a lot easier than a few of the other methods you may think of trying. In fact, there is an article here on 4Guys that illustrates how to use the SOAP Toolkit - Creating Web Services using ASP. (The Creating Web Services using ASP article shows how to produce and consume Web Services in classic ASP through the use of two VB COM components that utilize the SOAP Toolkit. In this article I'll demonstrate how to use the SOAP Toolkit to consume a .NET Web Service directly through a classic ASP page.)
The ASP page utilizes the Web Service we just created to calculate the discount for CompanyX for a
given product. To make coding and maintenance easier I like to remove the SOAP call
from the main bulk of the code and put it into its own function (
which is shown below. Since the
CalculateDiscount function uses the SOAP Toolkit, only
a few lines of code are needed to call the Web Service.
This function is fairly straightforward. The
mssoapinit call readies the Web Service
call by interrogating the WSDL of the Web Service. (For more information on what WSDL, read
Next, the actual Web method
CalculateDiscountCompanyX is called, passing in the
CostPrice parameters. The discount value is returned by the Web
Service and then returned by the
In the classic ASP page where the above function is found (
companyx_products.asp in the
code download), it is called once for every item in the
Products table. Essentially, the ASP page loops through the rows in the
Products table, calling the
CalculateDiscount function for each row and
displaying the discount for CompanyX.
This article has shown how one can build a classic ASP page to communicate with a .NET Web Service. Additionally, note that by placing business logic into a Web Service abstraction layer, one can have existing ASP applications on a Web server share the business logic rules with ASP.NET pages seamlessly.
Download the code (as an MSI file in ZIP format)