Billing on a Per-Use Basis

To make the price of a web service more attractive to potential buyers, you may want to consider pricing your web services on a per-use basis. You might, for example, bill a company 10 cents each time a program calls a service. To do so, you can implement a database that tracks the web service’s use on a license-key basis.

Depending on the billing model you choose, you might simply bill companies at the end of each month for each web service use. Second, you might ask companies to prepay for a specific number of uses. For example, you might require that a company pay a $50 deposit. Your software, in turn, would provide the company with 500 accesses to the service (based on the company’s license key). Then, each time the company calls one of the web service methods, your code can simply decrement the use counter. Later, after the company’s use count reaches a specific threshold, the web service can bill the company again in order to increase the company’s use count.

The PayPerUse web service in Listing 15.19 uses the XML file Users.xml to create a DataSet. The file’s contents are shown in Listing 15.18.

Listing 15.18 Users.xml

start example
<?xml version="1.0" standalone="yes"?> <DataSet>   <Table>     <Key>AAAA0000</Key>     <RemainingCalls>4</RemainingCalls>   </Table>   <Table>     <Key>AAAA0001</Key>     <RemainingCalls>2</RemainingCalls>   </Table> </DataSet>
end example

As you can see, the file specifies a license key number and the key’s remaining number of invocations. Before the requested method performs its processing, the code first examines the Remaining Calls field to determine the number of invocations the user has remaining. If the number is 0, the method generates an exception. Otherwise, the method decrements the count by 1 and performs the corresponding processing. In this case, the Demo method simply returns the string “Hello, world” if the user has not exceeded his allowed function invocations. Again, to run this web service, you must use IIS to enable Write permission on the folder that contains the file and then you must use Explorer to set the file’s permission to Write access for everyone.

To create the web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name PayPerUse. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 15.19.

Listing 15.19 PayPerUse.asmx.vb

start example
Public Function GetCount(ByVal Key As String, ByVal DataFile As _ Ä String) As Integer     Try       Dim DataSetObj As New DataSet()       DataSetObj.ReadXml(DataFile)       Dim Result As Integer = 0       Dim I As Integer       For I = 0 To DataSetObj.Tables(0).Rows.Count - 1         If (Key = DataSetObj.Tables(0).Rows(I).Item("Key")) Then           Result = DataSetObj.Tables(0).Rows(I).Item("RemainingCalls")           If (Result > 0) Then             DataSetObj.Tables(0).Rows(I).Item("RemainingCalls") = _ Ä           Result - 1             DataSetObj.WriteXml(DataFile)           End If         End If       Next       GetCount = Result       Catch Ex As Exception         Throw Ex       End Try  End Function  <WebMethod()> Public Function Demo(ByVal Key As String) As String      Dim Result As String = ""      Try        If (GetCount(Key, _          "C:\Inetpub\wwwroot\PayPerUse\bin\Users.xml") = 0) Then          Throw New Exception("License count is zero")        End If      Catch Ex As Exception        Throw Ex      End Try      Demo = "Hello, world"  End Function
end example

As you can see, the method calls the GetCount function to determine the number of method invocations the user has remaining. The GetCount function, in turn, uses the Users.xml file to create a DataSet object. The function then searches the object for the specified key. If the function matches the key, the function determines the number of calls the corresponding user has remaining. If that number is greater than 0, the function decrements the count and writes the updated DataSet object’s contents back out to the Users.xml file.

Again, for a real-world application, you would use a database as opposed to an XML-based file. However, using an XML-based file in this application lets you quickly test processing similar to that your web service might perform to bill users on a per-use basis.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

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