|
|
Many of the web services offered by companies such as Google or Amazon.com require that programmers who want to use the web service first register with the company in order to obtain a key the programmer can later use within his or her programs that uniquely identifies the programmer. If a program fails to specify a key, or a program specifies an invalid key, the service can generate an exception to which the program must respond.
To perform such processing, you might create an active server page that prompts each programmer to enter registration information (such as the programmer’s name and e-mail address) and which then returns a key that uniquely identifies the programmer. Behind the scenes, the active server page would likely store the programmer’s registration and key information within a database.
The RegisterService ASP.NET page in Listing 15.6 lets a user register to use a web service. When a user connects to the page, the browser will display a form similar to that shown in Figure 15.3, which prompts the user for registration information.
Figure 15.3: Prompting the user for registration information
After the user completes the form’s entries and then clicks on the Register button, the page will call the RegisterWS web service that registers the programmer and then returns a license key the programmer can later use in programs that call the web service. To create the RegisterService ASP.NET page, 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 Projects. Then, within the Templates field, click ASP.NET Web Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name RegisterService. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
Using your mouse, drag and drop the text fields and buttons previously shown in Figure 15.3 onto the page.
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.6.
Listing 15.6 RegisterService.asmx
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text.Length = 0 Then TextBox3.Text = "You must specify a name" ElseIf TextBox2.Text.Length = 0 Then TextBox3.Text = "You must specify a e-mail address" Else Try Dim WS As New localhost.Service1() TextBox3.Text = WS.RegisterUser(TextBox1.Text, TextBox2.Text) Catch Ex As Exception TextBox3.Text = Ex.Message End Try End If End Sub
As you can see, the code simply prompts the user to enter a name and e-mail address. Then, the code calls the RegisterUser method to register the user and to obtain a key the user can use within her programs that access the service. After you type the code, you must add a reference to the remote web service, by performing these steps (if you have not already created the RegisterWS web service discussed in the next section, do so now):
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type http://localhost/RegisterWS/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
While working through this chapter, you will create web services that make extensive use of databases (with the code built on the fly from XML files) to track user licenses. Each time a program calls one of these web services, the methods must first determine if the programmer has paid for full access to the service. If not, the code must then determine if the programmer has not yet purchased the web service. The code must then check if the programmer has registered to use the trial version. These additional database operations place overhead on the web service, which in turn slows the web service’s execution. Further, as the number of paid and trial-version programmers increases, so too will the overhead of testing for authorized users.
As an alternative to using license keys for paid access to a web service, some sites offer two versions of a web service. When programmers pay to upgrade a web service, a site will provide programmers with the WSDL entries for a full-access version of the service, which might not require programs to later use a license key to access the service. By eliminating the need for the license key, the web service will eliminate the overhead of database operations. However, the programmers must edit all existing programs that use the web service in order to link the program to the full-access web service. In addition, the web service’s developer loses control over which programs (or sites) can access the web service, because the code does not test for a license key. Should another programmer choose to violate the web service’s license agreement by providing the web service’s WSDL entries to another programmer, the developer may not detect that the service is being used by multiple sites.
In the previous section, you created the RegisterService ASP.NET page that sends a user’s registration information to the RegisterWS web service. Within the RegisterWS web service, the code stores the registration information within a database and then returns a unique key. If the programmer who is registering to use the service has previously registered, the code returns the programmer’s original key. Otherwise, if the programmer has not previously registered, the web service creates and returns a unique key, which it then returns to the programmer.
Again, for simplicity, the code uses XML-based files to create DataSet objects. Within a real-world application, you would want to use an actual database in order to improve functionality, performance, and security. To run the RegisterWS web service, you must use IIS to set Write permission for the folder that contains the PaidUser.xml and TrialUser.xml files.
To create the RegisterWS 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 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 RegisterWS. 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.7.
Listing 15.7 RegisterWS.asmx.vb
Imports System.Web.Services Imports System.IO <WebService(Namespace := "http://tempuri.org/")> _ Public Class Service1 Inherits System.Web.Services.WebService #Region " Web Services Designer Generated Code " ' Code not shown. #End Region Public Function TestIfRegistered(ByVal Email As String, ByVal _ Ä DataFile As String) As Boolean Try Dim DataSetObj As New DataSet() DataSetObj.ReadXml(DataFile) Dim Result As Boolean = False Dim I As Integer For I = 0 To DataSetObj.Tables(0).Rows.Count - 1 If (Email = DataSetObj.Tables(0).Rows(I).Item("Email")) Then Result = True End If Next TestIfRegistered = Result Catch Ex As Exception Throw Ex End Try End Function <WebMethod()> Public Function RegisterUser(ByVal Name As String, _ Ä ByVal Email As String) As String Dim Result As String = "" Try If (TestIfRegistered(Email, _ "C:\Inetpub\wwwroot\RegisterWS\bin\PaidUsers.xml")) Then Result = "Already registered" ElseIf (TestIfRegistered(Email, _ Ä "C:\Inetpub\wwwroot\RegisterWS\bin\TrialUsers.xml")) Then Result = "Already registered" End If Catch Ex As Exception Throw New Exception("File registration error") End Try If (Result.Length = 0) Then Try Dim DataSetObj As New DataSet() DataSetObj.ReadXml("C:\Inetpub\wwwroot\RegisterWS\bin\TrialUsers.xml") Dim LastRecord As Integer Dim LastKey As String Dim NumberPortion As String Dim LetterPortion As String Dim Number As Integer LastRecord = DataSetObj.Tables(0).Rows.Count - 1 LastKey = DataSetObj.Tables(0).Rows(LastRecord).Item("Key") NumberPortion = LastKey.Substring(4, 4) LetterPortion = LastKey.Substring(0, 4) If (NumberPortion = "9999") Then If (LetterPortion = "AAAA") Then LetterPortion = "BBBB" ElseIf (LetterPortion = "BBBB") Then LetterPortion = "CCCC" End If NumberPortion = "0000" Else Number = CInt(NumberPortion) Number = Number + 1 If Number < 10 Then NumberPortion = "000" ElseIf Number < 100 Then NumberPortion = "00" ElseIf Number < 1000 Then NumberPortion = "0" End If NumberPortion = NumberPortion & Number.ToString() End If Dim I As Integer Dim XMLFile As New _ Ä StreamWriter("C:\Inetpub\wwwroot\RegisterWS\bin\TrialUsers.xml") XMLFile.Write("<DataSet>") For I = 0 To DataSetObj.Tables(0).Rows.Count - 1 XMLFile.Write("<Table>") XMLFile.Write("<Name>") XMLFile.Write(DataSetObj.Tables(0).Rows(I).Item("Name")) XMLFile.Write("</Name>") XMLFile.Write("<Email>") XMLFile.Write(DataSetObj.Tables(0).Rows(I).Item("EMail")) XMLFile.Write("</Email>") XMLFile.Write("<Key>") XMLFile.Write(DataSetObj.Tables(0).Rows(I).Item("Key")) XMLFile.Write("</Key>") XMLFile.Write("</Table>") Next XMLFile.Write("<Table>") XMLFile.Write("<Name>") XMLFile.Write(Name) XMLFile.Write("</Name>") XMLFile.Write("<Email>") XMLFile.Write(Email) XMLFile.Write("</Email>") XMLFile.Write("<Key>") XMLFile.Write(LetterPortion & NumberPortion) XMLFile.Write("</Key>") XMLFile.Write("</Table>") XMLFile.Write("</DataSet>") XMLFile.Close() Result = LetterPortion & NumberPortion Catch Ex As Exception Throw Ex End Try Else If Result = "Already registered" Then Throw New Exception("User already registered") Else Throw New Exception("Registration error") End If End If RegisterUser = Result End Function End Class
The web service makes use of the TestIfRegistered function to determine if the user trying to register is already registered as either a paid or trial user. The function searches the DataSet object for a matching e-mail address. If the function finds a match, it returns the value true. The web service, in turn, generates an exception that signifies the user is already registered. If the TestIfRegistered function returns false, the web service creates a unique key for the new user. To do so, the web service examines the last entry within the Trial version DataSet to determine the number of the last key assigned. The web service then increments the key value. If the previous key value ended with the value 9999, the service sets the numeric portion of the key to 0000 and then changes the text portion from AAAA to BBBB or from BBBB to CCCC. In this way, the web service supports up to 30,000 keys in the range AAAA0000 to CCCC9999.
|
|