Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Saturday, October 11, 2008

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Adding Web Services to Your ASP.Net Application
12/7/2003 1:09:55 PM

In this article we will cover exposing an existing ASP.Net application as a Web service. We will provide methods for creating the Web service, converting readers to data sets, and a client application for the new service.

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.

By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 10/19/2005 3:39:29 AM by Anonymous
Comments: A good thing to start with. As some one has already posted - short and sweet. Good things come in small packages.
Left on 2/10/2005 6:33:40 AM by Anonymous
Comments: I knew nothing about wesb services and i must this was good foundation layer tutorial
No ratings available.
Left on 10/29/2004 5:31:50 AM by Anonymous
Comments: Very enlightening. Short and Sweet.
No ratings available.
Left on 5/31/2004 11:18:31 PM by Anonymous
Comments: I'd like to know how to connect to a web service using ASP.Net without the IDE as well.
No ratings available.
Left on 5/26/2004 5:27:02 AM by Anonymous
Comments: Good on you mite, you saved my crucial time!!
Left on 12/22/2003 4:24:33 AM by Anonymous
Comments: worst
Left on 12/17/2003 8:51:39 AM by Anonymous
Comments: It would likely be a good idea to give quick mention to the process of referencing a WebService without an IDE such as Visual
Studio.Net. 

That is, how and where do you write the code to reference the service?
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970