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 |
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 |
|
<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 |
You may test the web service by clicking here.
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> |
You may run the above program by clicking Here.
You may download the code by clicking
Here.