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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.
Published: Wednesday, July 03, 2002

Integrating ASP.NET XML Web Services with 'Classic' ASP Applications

By Paul Bunting


Introduction
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?

- continued -

'

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!

The Database
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 code download.)

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 file (named eCommerce.asmx - the Web Service itself being named eCommerce). Within the Web Service file change the default namespace to ClassicASP_nDotNET.eCommerce and then add the Web method CalculateDiscountCompanyX.

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 CalculateDiscount(CompanyID), where 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).

The CalculateDiscountCompanyX Web method is fairly simple, taking the following input parameters:

  1. UnitPrice - the recommended retail price (RRP) (type Double)
  2. CostPrice - the actual cost of the product (i.e., how much our company had to pay to get the product) (type Double)
  3. AccessCode - a password for security of web service (type String)

Upon error the CalculateDiscountCompanyX Web method returns a negative value. The source code for the method can be seen below:

' the web method
<WebMethod()> _
Public Function CalculateDiscountCompanyX(ByVal UnitPrice As Double, _
                                          ByVal CostPrice As Double, _
                                          ByVal AccessCode As String) As Double
    Dim dblDiscount As Double, _
      dblMarkup As Double, _
      dblMinMarkup As Double, _
      dblMinClientDiscount As Double

    dblMarkup = (1.3)  ' our desired markup percentage (30%)
    dblMinMarkup = (1.075)  ' our min markup (7.5%)
    
    ' the desired minimum discount for our customer (10%)
    dblMinClientDiscount = (0.1)

    Try
      If AccessCode = "p455w0rd" Then
        ' dblDiscount - Calculates discount based on the markup percentage
        ' supplied.
        dblDiscount = FormatNumber(((UnitPrice - (CostPrice * dblMarkup))), 2)

        ' check discount is greater than the minimum
        If dblDiscount + 0 <= (UnitPrice * dblMinClientDiscount) Then
          dblDiscount = FormatNumber((UnitPrice * dblMinClientDiscount), 2)
        End If

        ' Check price does not go too low - if it is 
        ' make sure we get our min margin
        If (UnitPrice - dblDiscount) <= (CostPrice * dblMinMarkup) Then
          dblDiscount = FormatNumber((UnitPrice - (CostPrice * dblMinMarkup)), 2)
        End If

        Return dblDiscount
      Else
        Return -1   'invalid password
      End If
    Catch ex As Exception
      Return -1   'eep, an exception was thrown
    End Try
End Function

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 (CalculateDiscount), 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.

Public Function CalculateDiscount(UnitPrice, CostPrice)
  SET objSoapClient = Server.CreateObject("MSSOAP.SoapClient")
  objSoapClient.ClientProperty("ServerHTTPRequest") = True
	  
  ' needs to be updated with the url of your Web Service WSDL and is
  ' followed by the Web Service name
  Call objSoapClient.mssoapinit("http://localhost/ClassicASP_n_DotNET/" & _
                                         "eCommerce.asmx?WSDL", "eCommerce")
	  
  ' use the SOAP object to call the Web Method Required  
  CalculateDiscount = objSoapClient.CalculateDiscountCompanyX(UnitPrice, _
                                                      CostPrice, "p455w0rd")
End Function

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 this article.) Next, the actual Web method CalculateDiscountCompanyX is called, passing in the UnitPrice and CostPrice parameters. The discount value is returned by the Web Service and then returned by the CalculateDiscount function.

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 Northwinds 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.

Conclusion
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.

Happy Programming!

  • By Paul Bunting
    http://www.developtheweb.co.uk/


    Attachments
    Download the code (as an MSI file in ZIP format)



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