Restricting a Web Service to 30 Days Use

Restricting a Web Service to 30 Days’ Use

One way to provide programmers with ample time to test your web service is to provide programmers with access to the web service for a 30-day period. To implement a 30-day limit,

you would need to store the date that the user registers within a database. The 30DayLimit web service in Listing 15.13 relies on the registration information stored within the LimitUsers.xml file in Listing 15.12. The file contains a key and a start date.

Listing 15.12 LimitUsers.xml

start example
<?xml version="1.0" standalone="yes"?> <DataSet>   <Table>     <Key>AAAA0000</Key>     <Month>11</Month>     <Day>05</Day>     <Year>2002</Year>   </Table>   <Table>     <Key>AAAA0001</Key>     <Month>10</Month>     <Day>07</Day>     <Year>2002</Year>   </Table> </DataSet>
end example

The service performs one method that returns a famous quote. After the registered user’s 30-day period expires, the service will no longer return a quote, but rather, will generate the TrialPeriodExpired exception. To create the 30DayLimit 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 .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 30DayLimit. 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.13.

Listing 15.13 30DayLimit.asmx.vb

start example
Public Function GetDays(ByVal Key As String, ByVal DataFile As _    String) As Integer    Try       Dim DataSetObj As New DataSet()       DataSetObj.ReadXml(DataFile)       Dim Result As System.TimeSpan       Dim I As Integer       For I = 0 To DataSetObj.Tables(0).Rows.Count - 1         If (Key = DataSetObj.Tables(0).Rows(I).Item("Key")) Then           Dim RegistrationDate As New _ Ä         DateTime(DataSetObj.Tables(0).Rows(I).Item("Year"), _                      DataSetObj.Tables(0).Rows(I).Item("Month"), _                      DataSetObj.Tables(0).Rows(I).Item("Day"))             Result = Now.Subtract(RegistrationDate)         End If        Next        GetDays = Result.Days      Catch Ex As Exception        Throw Ex    End Try End Function <WebMethod()> Public Function GetQuote(ByVal Key As String) As String    Try      If (GetDays(Key, _          "C:\Inetpub\wwwroot\30DayLimit\bin\LimitUsers.xml") > 30) Then        Throw New Exception("Expired or invalid license")      End If    Catch Ex As Exception      Throw Ex    End Try    GetQuote = "Great spirits have always encountered violent " & _ Ä   "opposition from mediocre minds. Albert Einstein" End Function
end example

Each time a program calls the web service GetQuote method, the code calls the GetDays function to determine the number of days that have passed since the user registered to use the service. If the number of days exceeds 30, the web service generates an exception. If the number of days is 30 or less, the web service returns a quote.

. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa © 2008-2017.
If you may any questions please contact us: