Microsoft has put a large amount of funds to promote its various advantages of its .NET, one of the biggest advantages of .NET is Microsoft's start-up XML Web Services. XML Web Services can separate your programs and previous custom DLLs and COM / DCOM (avoid falling into a DLL trap), then design these features into XML Web Services, allowing other applications to access and call these features (such as: Website, Distributed Program) (For how to establish and use XML Web Services in .NET, please refer to this article: http://aspnet.4guysfromrolla.com/articles/062602-1.ASPX). Then, when you consider upgrading the existing ASP program performance, how do you combine it with the developed XML Web Services?
The purpose of this article is to demonstrate how to combine the latest XML Web Services and existing ASP programs. The existing ASP programs can be "shared" XML Web Services like the ASP.NET program by writing some of the specific business functions to XML Web Services and the XML Web Services and ASP programs. When you need to improve the performance of the existing ASP program and synchronize the ASP.NET that has been developed, there is no time and effort to completely modify these programs to the ASP.NET program, we can consider combining ASP and XML Web Services. use.
The examples that this article will use are based on the following real needs: in B2B e-commerce, we may have some special discounts for some relatively fixed customers. Here we will build a Web Services, this web service is to calculate discounts for special customers, and then we combine it with a simple ASP program.
database
We use the Northwind database from Microsoft Access, of course, we don't pay close, we need to make some small modifications to the "Products" data table structure of the database: add a list of actual prices for stocks. Costprice field (we will fill in some random data) (this database can be downloaded here): http://www.4guysfromrolla.com/webtech/code/asptonet.zip)
Web service
The article "http://aspnet.4guysfromrolla.com/articles/062602-1.aspx" in the article "CREATING AND CONSUMING A Web Service", we can use the text editor to create web services, such as Web Matrix Projext Or VS.NET and other tools. We will use vs.net to establish Web Service using VS.NET.
First, create a new ASP.NET project in VS.NET, then delete the program created Web Form, add a new web service file (named: ecommerce.asmx), in this web service, will default The name space is modified to classicasp_ndotnet.ecommerce and add a method called CalculatedInscountCompanyx.
Web method CalculateDiscountCompanyX is a simple function for computing company companiesx discounts (of course, more robust solution is to write a universal function such as: CalculateDiscount (CompanyID), in this function, COMPANYID specifically used to define specific company numbers, Then, the program will find the specific company name according to the company number and make discounts. We mainly purpose here is to demonstrate the combination of XML Web Services and ASP programs. Therefore, only some simple functions are required.) In our application, it is assumed that the minimum discount for the company companiesX is 10%, and the company's lowest pure profit is 7.5%, and these specific discounts and profits are cured written in the program.
Web method CalculateDiscountCompanyx is quite simple and will use some of the following parameters:
1. Unitprice: Recommended retail price, data type is Double;
2, Costprice: The actual price of the goods is also the purchase price, the data type is Double;
3, AccessCode: The security password of the web service, the data type is string;
If the function calculates an error, -1 is returned, the following is the function source code:
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) 'expect profit
DBLMINMARKUP = (1.075) 'Minimum profit
'The lowest discount for customers
DBLMINCLIENTDISCOUNT = (0.1)
Try
If AccessCode = "p455w0rd" then
'Calculate discounts according to profit
DBLDiscount = FormatNumber ((unitprice * dblmarkup)), 2)
'Check if the discount is suitable NA
IF dbldiscount 0 <= (Unitprice * dblminClientDiscount) THEN
DBLDISCOUNT = FormatNumber (UNITPRICE * DBLMINCLIENTDISCOUNT), 2)
END IF
'Check if there is a minimum profit
IF (unitprice - dbldiscount) <= (Costprice * dblminmarkup) THEN
DBLDISCOUNT = FormatNumber ((Unitprice - (Costprice * DBLminmarkup), 2)
END IF
Return DBLDiscount
Else
Return -1 'password error
END IF
Catch exception
Return -1
END TRY
END FUNCTION
Combine Web Services and ASP programs
In order to make ASP and Web Services, communication is relatively simple, you need to download the MS SOAP Toolkit 2.0 (address is: http:/msdn.microsoft.com/soap/) so you can use SOAP to implement ASP and Web Services's communication, which will make the ASP program and Web Services communication becomes simpler than using other methods. In fact, there is an article in the 4Guys website to introduce SOAP usage. This article is: CREATING Web Services Using ASP (Address: http://www.4guysfromrolla.com/webtech/070401-1.shtml). In this way, the ASP page can use the Web Service Comparative Company's commercial discount using the Web Service computing company, in order to make the program look simpler and regulations, we move the SOAP call from the main program, specifically write a function: CalculateDiscount. Due to the use of SOAP, this function calls the Web Service is relatively simple, only a few lines of code, the program code is as follows:
Public Function CalculateDiscount (Unitprice, Costprice)
Set objsoapclient = server.createObject ("mssoap.soapclient")
ObjsoapClient.ClientProperty ("ServerHttpRequest") = TRUE
'The link below needs to be modified to our specific website link
Call objsoapclient.msoapinit ("http: // localhost / classicasp_n_dotnet /" "& _
"ecommerce.asmx? wsdl", "ecommerce")
'Call the Web Service function using the SOAP object
CalculateDiscount = ObjsoapClient.calculated (Unitprice, _
Costprice, "P455W0RD")
END FUNCTION
The above procedures are quite clear. First, the MSSOAPinit method uses WSDL (for WSDL, please refer to: http://samples.gotdotnet.com/quickstart/aspplus/doc/webserviceSintro.aspx) to prepare for calling Web Service, and then truly calculate discounts The function CalculateDiscountCompanyx is called, calculates the discount and returns to the function CalculateDiscount.
In the source code we provide, the function can be found in the companyX_Products.asp page, the ASP page will traverse the entire "Products" data table. When the reality is recorded, the "CalculatedInscount" function will be called to calculate the company. Companyx's discount.
in conclusion
The article describes how to use ASP and Web Service communication, in addition, we can know that some functional modules can be used to call these modules like ASP.NET.
I wish you a happy programming and download the above program code (note the download decompression is the MSI format file).