The Web Service

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Week 3.  In Review


This Web Service will allow payees to remotely post transactions to your account. For example, if you use a debit card at a restaurant, the restaurant could access this Web Service and determine immediately whether you could pay.

Thanks to the Account business object, this Web Service is fairly easy to create. Listing BP3.6 shows the code.

Listing BP3.6 The Web Service Uses the Existing Account Object
 1:  <%@ WebService Language="VB"  %> 2: 3:  Imports System 4:  Imports System.Data 5:  Imports System.Data.OleDb 6:  Imports System.Web.Services 7: 8:  public Class Bills : Inherits WebService 9:     private objConn As New OleDbConnection("Provider=" & _ 10:        "Microsoft.Jet.OLEDB.4.0;" & _ 11:        "Data Source=c:\ASPNET\data\banking.mdb") 12: 13:     <WebMethod()> Public function PostBill(UserID as Integer, strPayee as string,  graphics/ccc.gifdecAmount as Decimal) as String 14:        dim objAccount as New ASPNETBank.Account( _ 15:           UserID.ToString) 16:        dim decBalance as Decimal = objAccount.GetBalance 17: 18:        if decAmount <= decBalance then 19:           objAccount.UpdateBalance(decBalance - decAmount) 20:           objAccount.AddTransaction(strPayee, decAmount) 21:           return "Success" 22:        else 23:           return "Insufficient funds" 24:        end if 25:     end function 26:  End Class 

graphics/analysis_icon.gif

Save this file as PostTransaction.asmx. Line 1 declares that this file is a Web Service, and that the Web Service class is Bills, which is declared on line 8. Note that it inherits from the WebService class. Line 9 declares an OleDbConnection object for use with your only method, PostBill, on line 13.

The rest of the code should look very similar to the PayBill method from Listing BP3.3. The PostBill method, however, takes a few more parameters; specifically, parameters that declare the UserID (and consequently the account ID), the merchant who is posting the bill, and the amount of the bill. Note that it also returns a string you'll get to that in a moment. Line 14 creates a new Account object with the specified user ID, and line 16 retrieves the current balance from the account.

Line 18 determines whether the amount of the posted bill is less than the current balance. If so, the account is updated with the new balance and transaction. If the transaction is successful, you return the string "Success" to the merchant. That way, the merchant has a definite confirmation from you. If the transaction fails, you return a reason why this is why returning a string from this method is helpful. Rather than the service simply saying that the transaction failed, you can give detailed information on why it failed, using the returned string.

That's all there is to it! Figures BP3.3 and BP3.4 show a test of this Web Service and the results generated.

Figure BP3.3. Testing the Web Service with Http-Post.

graphics/bp03fig03.gif

Figure BP3.4. The XML results generated from the text.

graphics/bp03fig04.gif


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net