In this code snippet we are going to provide you with a way to expose your
existing ASP.Net application so it can be exposed as a Web service. If you're
not familiar with Web services, they provide you with a means to expose the
functionality of your application to other clients over the Web. For example,
here at WWWCoder.com we provide our directory as a Web service to clients so
they can integrate the directory into their portal site. Here is an
online example
of what I'm referring to. You can see from that site, they are using our
directory integrated into the look and feel in there site. All data is being
feed by our Web service hosted here at WWWCoder.com. We provide a module snap-in
for their DotNetNuke portal so they can integrate the directory into their site.
What we're going to do is add a Web service file to our existing application
that we want to expose, and then create another project which will be the
consumer of our application.
How to Create a Web Service
The first thing you need to do is create an asmx file to expose your
application over the Web. Open your Visual Studio.Net project and create a new
Web service.
Imports System.Web.Services
::
::
<System.Web.Services.WebService(Namespace:="http://tempuri.org//MyWebService")>
_
Public Class MyWebService
Inherits System.Web.Services.WebService
::
::
::
End Class
Now if you wish to expose a method as a Web service you can do so in the
following way, in this example we will expose a part of the application that is
going to obtain a category listing of our directory.:
<WebMethod()> _
Public Function GetCategories(ByVal inParentID As Integer) As DataSet
Dim objCategories As New ASPSearchDB
'now call our method that gets our datareader and converts it to a dataset.
Dim dsCategories As DataSet = _
ConvertDataReaderToDataSet(objCategories.GetCategories(thisModule, _
inParentID, True))
Return dsCategories
End Function
You'll notice we make a call to a method called ConvertDataReaderToDataSet; you
cannot return a data reader from a Web service, many applications use the data
reader to return results or bind to a datagrid due to performance gains. Web
services however cannot return a data reader, readers will need to be converted
in order to return the results to the client of your service. Here we have the
conversion method to return a dataset to the client from our service.
Public Function ConvertDataReaderToDataSet(ByVal reader As _
SqlDataReader) As DataSet
Dim dataSet As dataSet = New dataSet
Dim schemaTable As dataTable = reader.GetSchemaTable()
Dim dataTable As dataTable = New dataTable
Dim intCounter As Integer
For intCounter = 0 To schemaTable.Rows.Count - 1
Dim dataRow As DataRow = schemaTable.Rows(intCounter)
Dim columnName As String = CType(dataRow("ColumnName"), String)
Dim column As DataColumn = New DataColumn(columnName, _
CType(dataRow("DataType"), Type))
dataTable.Columns.Add(column)
Next
dataSet.Tables.Add(dataTable)
While reader.Read()
Dim dataRow As DataRow = dataTable.NewRow()
For intCounter = 0 To reader.FieldCount - 1
dataRow(intCounter) = reader.GetValue(intCounter)
Next
dataTable.Rows.Add(dataRow)
End While
Return dataSet
End Function
What we
do in the previous method is accept the reader that we want to convert to a
dataset. We then declare a new dataset to store our values from the reader
object and then structure our dataset with a table. Once a table is created then
go through the schema of the reader and create our data table structure based on
the reader's structure by looping through the columns and adding them to our new
table. Once the structure of the new table is defined we then itterate through
the rows in the reader and add the data that is contained within the reader.
Finally add the new table to the dataset and then return the newly created
dataset.On the Client
Now we have a Web service wrapper around our application, we need to create a
consumer for our data. You can start out by creating a Web project in Visual
Studio.Net as you would normally and then add a Web reference to the Web
service. You do this in Visual Studio, by right clicking on your project
references and select Web Reference. You'll then be prompted to enter in a URL
of your Web service class file. Once the reference is created you can then work
with it in Visual Studio like any other object. Intellisense and all.
Then in your code behind for your aspx page you would add a reference in the
page like so:
Dim objWS As New com.wwwcoder.www.MyWebService
Now that the MyWebService is instantiated in our class we can refer to the
previous method GetCategories and bind the resulting Data Set to a Data List
like so:
::
::
DataList1.DataSource = objWS.GetCategories(CType(viewstate("parentid"), _
Integer))
DataList1.DataBind()
This should be enough to get you started with working with Web services.