.NET Web Services Solutions

After programmers have an opportunity to “test drive” the trial version of your web service, you must provide the programmers with a way to upgrade to a full-function version of the web service. The UpgradeWebService ASP.NET page in Listing 15.8 lets a user upgrade a web service from a trial version. When a user connects to the page, the browser will display a form similar to that shown in Figure 15.4.

Figure 15.4: Allowing a user to upgrade a web service

As you can see, the form prompts the user to enter his license key. (For a real-world application, you would also prompt the user for credit-card information that you could validate and then bill.) Next, the form prompts the user to enter credit-card information. After the user enters valid information, the code will display a message to the user telling him that the web service has been upgraded and is now fully functional.

To create the UpgradeWebService ASP.NET page, 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 Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name UpgradeWebService. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Using your mouse, drag and drop the text fields and buttons previously shown in Figure 15.4 onto the page.

  4. 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.8.

Listing 15.8 UpgradeWebService.aspx.vb

Private Sub Button1_Click(ByVal sender As System.Object, ÄByVal e As _ System.EventArgs) Handles Button1.Click Dim Result As String Try If (TextBox1.Text.Length > 0) Then Dim WS As New localhost.Service1() Result = WS.UpgradeLicense(TextBox1.Text) TextBox2.Text = Result End If Catch Ex As ExecutionEngineException TextBox2.Text = Ex.Message End Try End Sub

After the user enters his e-mail address and clicks the Upgrade License button, the code calls the UpgradeWS web service UpgradeLicense method. In a real-world application, your code would likely prompt the user for credit-card information, which it would then use for billing.

As you will see, the UpgradeWS web service’s code (Listing 15.9) does not change the programmer’s license key, but rather, lets the programmer continue to use the original key. That way, the programmer does not have to change any programs that currently use the web service. 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. In addition, you must use Explorer to set the PaidUser.xml file to allow Write access for everyone.

To create the UpgradeWS 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 UpgradeWS. 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.9.

Listing 15.9 UpgradeWS.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, ByRef Name As String, ÄByRef Key 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 Name = DataSetObj.Tables(0).Rows(I).Item("Name") Key = DataSetObj.Tables(0).Rows(I).Item("Key") Result = True End If Next TestIfRegistered = Result Catch Ex As Exception Throw Ex End Try End Function Public Function CopyUser(ByVal Name As String, ByVal Email As String, _ ÄByVal Key As String, ByVal Filename As String) As String Try Dim DataSetObj As New DataSet() DataSetObj.ReadXml("C:\Inetpub\wwwroot\UpgradeWS\bin\PaidUsers.xml") Dim I As Integer Dim XMLFile As New StreamWriter(Filename) 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(Key) XMLFile.Write("</Key>") XMLFile.Write("</Table>") XMLFile.Write("</DataSet>") XMLFile.Close() CopyUser = "User copied to Paid.xml" Catch Ex As Exception Throw Ex End Try End Function <WebMethod()> Public Function UpgradeLicense(ByVal Email) As String Dim Name As String Dim Key As String Try If (TestIfRegistered(Email, Name, Key, _ "C:\Inetpub\wwwroot\UpgradeWS\bin\TrialUsers.xml")) Then CopyUser(Email, Name, Key, _ "C:\Inetpub\wwwroot\UpgradeWS\bin\PaidUsers.xml") End If Catch Ex As Exception Throw New Exception("File registration error") End Try UpgradeLicense = "License upgraded" End Function End Class

The web service code copies the user’s license information to the PaidUsers.xml file. For simplicity, the code does not remove the user’s license information from the TrialUsers.xml file. If the code were to remove the user from the TrialUsers.xml file, the code would also need to test for no remaining entries within the file. In addition, the code that creates the unique user key is based on the last key number assigned. If the UpgradeWS web service were to move the last entry in the TrialUsers.xml file, you would need to change the code that determines the new license key to search both XML files.

Категории