Build a Web Service and Create a Consumer...
By: John Kilgo Date: December 15, 2002 Download the code.


In this article we will create a guest book web service that will control additions to, and displays from a database table. We will also create a consumer (a regular Web Form) of the web service. The consumer is discussed at the end of the article. Web services look a little strange at first glance but are fairly straight forward once you get used to them.

The top of a web service must look exactly like the code shown below. The Importing of namespaces will vary, however, depending upon exactly what your are going to do with the service. You must Import System.Web.Services and since we will be dealing with a SQL Server database we also need System.Data and System.Data.SqlClient. You must then define the main class. We do that with the last line of code shown below. The last line also serves to establish the path to the web service file. In the example below, the web service will live in the services directory below the root.

<%@ WebService Language="VB" Class="GuestBook"%>
Option Strict Off
Option Explicit On

Imports System.Web.Services
Imports System.Data
Imports System.Data.SqlClient
<WebService(Namespace:="http://localhost/wwwroot/dotnetjohn/services/")> Public Class GuestBook
Next we create one or more methods of the service which will determine exactly what the web service does. Methods can be Subs or Functions, the difference being of course, that Functions return values while Subs do not. We will create one of each.

First we create a Sub method called AddGuest which contains two input parameters ( FirstName and LastName). We then set up our database connection to handle a command which inserts the two names plus the system datetime into our table. Where do the FirstName and LastName come from? They could come from a web form consumer of our service which might contain textboxes for the the names. The consumer would then instantiate the guestbook service and call its AddGuest method. You will also see shortly that .Net provides a nice, xml-based, web service testing platform. That can also be used to enter the names.

<WebMethod()> Public Sub AddGuest (FirstName as String, LastName as String)
Dim sqlConn as sqlConnection
Dim sqlCmd As New sqlCommand
sqlConn = New SqlConnection("server=localhost;database=dotnetjohn;uid=sa;pwd=;")
sqlCmd.Connection = sqlConn
sqlCmd.Connection.Open
sqlCmd.CommandText = "Insert Into dnt_guests " _
& "(FirstName, LastName, VisitDate) Values (" _
& "'" & FirstName & "', " _
& "'" & LastName & "', GetDate())"
sqlCmd.ExecuteNonQuery()
End Sub
Next, and last, is our function which executes a SELECT statement to return the guestbook data. The first third of the code below is just setting up the database connection to return all rows of the guest book. The last two-thirds of the code is an attempt to make the output look pretty for the consumer of the web service method. Instead of just returning raw data that might not end up very well formatted, we create an html table to contain the data. That way, all the data will appear nicely formatted when the method is called.
<WebMethod()> Public Function GetGuests as String
Dim sqlConn as sqlConnection
Dim dataAdapter As sqlDataAdapter
Dim DS as New DataSet
Dim row As Integer
sqlConn = New SqlConnection("server=localhost;database=dotnetjohn;uid=sa;pwd=;")
dataAdapter = New sqlDataAdapter("Select * From dnt_guests Order By VisitDate", sqlConn)
dataAdapter.Fill(DS, "guests")
GetGuests = "<table border='1' bordercolor='Indigo' bgcolor='Ivory'>"
GetGuests &= "<tr><th colspan='3'>Guest Book</th></tr>"
GetGuests &= "<tr><td>First Name</td><td>Last Name</td>"
GetGuests &= "<td>Date Visited</td></tr>"

For row = 0 To ds.Tables(0).Rows.Count - 1
GetGuests = GetGuests _
& "<tr><td>" & DS.Tables(0).Rows(row).Item("FirstName") _
& "</td><td>" & DS.Tables(0).Rows(row).Item("LastName") _
& "</td><td>" & DS.Tables(0).Rows(row).Item("VisitDate") _
& "</td></tr>"
Next
GetGuests &= "</table>"
End Function End Class
As stated earlier, .Net provides a way to test our web service. When you click on the following link you will see links for the two methods we have created. If you select AddGuests you will be presented with two textboxes for the first and last names as well as a submit button. If you select GetGuests you will be presented with a submit button. Click the submit button and you will see all of the data returned by the GetGuests method returned as xml. It is not pretty to look at, but it will show all the data. Remember that I did not write any code for this to work. .Net provides this functionality automatically when you view a web service (.asmx) file.

You may test the web service by clicking here.


If you clicked on the link above you can see that .Net provides a very nice and convenient way to test your web services. Some people leave it at that and just present the returned data in xml format. It gets the job done, but is not very pretty, or in some cases readable.

Once someone knows about your service he or she can utilize its methods in their own program. Before that can happen, however, we must do a couple of things with our web service. First we must create a class (a .vb program) from the web service. We do that using the WSDL utility program. The wsdl utility is called from the directory where your consuming .aspx page will be. It is called as follows: wsdl.exe /language:VB http://www.dotnetjohn.com/services/GuestBook.asmx?WSDL. The latter part tells WSDL where to find the .asmx file. You would, of course, substitute your path (which may start at localhost rather than an external web site) to the file rather than the www.dotnetjohn.com shown above.

Once we have the class that WSDL produces, we must compile it and place the resulting .dll in a bin directory beneath the directory where the consuming .aspx page will run. We compile it using the following line in a command window: vbc /out:bin/guestbook.dll /t:library /r:System.xml.dll, System.web.services.dll, system.dll guestbook.vb. Now we are finally able to create and run a web form which consumes our web service.

The following page (ShowGuests.aspx) instantiates the web service and then calls its GetGuests method to return the DataSet of information from the guestbook table. As you can see, this is a very simple little program. It can be simple because the web service is doing most of the work.

<%@ Page Language=VB %>

<Script runat=server>
Private strReturn as String
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
Dim myBook As New GuestBook()

strReturn = myBook.GetGuests()
Response.Write(strReturn)
End Sub
</Script>

<html>
<head>
<title>ShowGuests</title>
</head>
<body>
</body>
</html>
Conclusion: In this article you have seen how to create a web service with two methods, how to test the service simply by loading the .asmx file into the browser, how to use the WSDL utility from a command window to create VB class file, how to compile the class file into a dll, and how to create a web form consumber of the web service.

You may run the above program by clicking Here.
You may download the code by clicking Here.