To restrict program access to a trial version of a web service, many web services will limit the number of method calls to the service per day. The Google web service, for example, which you examined in Chapter 3, “Accessing Web Services from within HTML Pages,” limits the number of program calls to 1,000 per day. The 500Calls web service in Listing 15.17 limits programs to 500 calls per day using a specific license key. The web service provides two methods. The first simply returns a count of the number of calls the license key specified has made today. The second resets the number of calls back to 0. An application might call the ResetCount method at the start of each day. If programs call the web service more than 500 times during the same day using a specific license key, the web service will generate the CallLimitExceeded exception.
The program uses the DayCount.xml file in Listing 15.16 to track the number of calls per day.
Listing 15.16 DayCount.xml
<?xml version="1.0" standalone="yes"?> <DataSet> <Table> <Key>AAAA0000</Key> <TodaysCalls>4</TodaysCalls> </Table> <Table> <Key>AAAA0001</Key> <TodaysCalls>498</TodaysCalls> </Table> </DataSet>
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 DayCount.xml file’s permission to Write access for everyone. To create the 500Calls web service, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic .NET 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 500Calls. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
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.17.
Listing 15.17 500Class.asmx.vb
Public Function GetCallCount(ByVal Key As String, ByVal DataFile As _ String) As Integer Try Dim DataSetObj As New DataSet() DataSetObj.ReadXml(DataFile) Dim Result As Integer = 500 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("TodaysCalls") If (Result < 500) Then DataSetObj.Tables(0).Rows(I).Item("TodaysCalls") = _ Result + 1 DataSetObj.WriteXml(DataFile) End If End If Next GetCallCount = Result Catch Ex As Exception Throw Ex End Try End Function <WebMethod()> Public Function UseCount(ByVal Key As String) As Integer Dim Result As Integer = 0 Try Result = GetCallCount(Key, _ "C:\Inetpub\wwwroot\500Calls\bin\DayCount.xml") If (Result >= 500) Then Throw New Exception("License count exceeded") End If Catch Ex As Exception Throw Ex End Try UseCount = Result End Function <WebMethod()> Public Function ResetCallCount(ByVal Key As String, _ ÄByVal DataFile As String) As Integer Try Dim DataSetObj As New DataSet() DataSetObj.ReadXml(DataFile) Dim I As Integer For I = 0 To DataSetObj.Tables(0).Rows.Count - 1 DataSetObj.Tables(0).Rows(I).Item("TodaysCalls") = 0 Next DataSetObj.WriteXml(DataFile) ResetCallCount = 0 Catch Ex As Exception Throw Ex End Try End Function
As you can see, when a program calls the UseCount method, the code calls the GetCallCount method that uses the XML file to create a DataSet object. The function then searches the DataSet for the corresponding key value. If the function finds the key, it returns the number of times the key has been used today (actually since the last reset operation). If the key is not found, the function simply returns the value 500, which corresponds to exceeded use count. The web service also provides the ResetCallCount method that resets the use count for each key.