With some help from the great guys from Secure Webs, I decided to expose my site,
http://www.123aspx.com , as a web service. I wanted to start with something simple, so I
decided to expose the "What's New" ASP.NET resources. The "what's new"
section contains the latest 12 additions to my site. Here is a quick tutorial around
writing that webservice.
Webservices in ASP.NET are built around the SOAP (Simple Object
Access Protocol) and WSDL (Web Services Description Language). We're not going to get into these
standards (WSDL, and SOAP), but instead, focus on creating a webservice and consuming it.
I decided to return a dataset object as my collection of new resources. I chose a dataset
because most ASP.NET developers are already familiar with datasets, and they can easily be
bound to datagrids. The dataset consists of 4 columns:
Name - the name or title of the resource.
URL - The url to the resource
Domain - The domain name the resource can be found at.
DateUpdated - The date the resource was updated
We start programming a webservice by declaring it to the .NET engine and importing the
| <%@ WebService
Language="VB" Class="AspX123WebSvc" %>
Option Strict On
Option Explicit On
|We also need to tell the compiler that the class
"AspX123WebSvc" will be webservice enabled. We do this by inheriting the
WebService namespace in the class declaration.
|Public Class AspX123WebSvc : Inherits WebService
|Now that we have our classes defined, I went ahead and declared the main
Getting To It
Because we are declaring a method here, we need to mark it as a webservice method
| Public Function GetNewResources() As DataSet
|I decided to add a friendly description to this method, to tell the consumer what this method does.
When we view the default WSDL, supplied natively by ASP.NET, our description will show be available to the consuming programer.
Once we have our functions and classes declared, writing a webservice is just like writing any other codebehind file.
Accessing the Database
|Now that I have my webservice framework in place, let's go ahead and get our our data. In this example, I need to
massage the data a little bit, specifically the domain name of the ASP.NET resource. So what I decided to do,
was to return a datareader, strip off only the domain name of the resource (instead of returning the complete url), and then
build the dataset that we will eventually be returning. To access the database I use 2 utility functions. One function is called GetDataReader( )
and the other function is called sqlConnString(). . GetDataReader() returns a SqlDataReader, it also takes advantage of System.Data.CommandBehavior.CloseConnection.
System.Data.CommandBehavior.CloseConnection is a parameter that tells the framework to close the datareader as soon as I'm done reading from it.
sqlConnString() is used to read my SQL Server connection string from the web.config file. I've included a snippet from my web.config file to
display how I'm adding an appsettings section to web.config.
Private Function GetDataReader(sqlText as String) as SqlDataReader
Dim dr as SqlDataReader
Dim sqlConn as SqlConnection = new SqlConnection( sqlConnString() )
Dim sqlCmd as SqlCommand = new SqlCommand( sqlText, sqlConn )
dr = sqlCmd.ExecuteReader( System.Data.CommandBehavior.CloseConnection )
Private Function sqlConnString() as String
<add key="WebSvcDb" value="Password=;User ID=sa;Initial Catalog=pubs;Data Source=127.0.0.1;" />
Getting the Data
|I have a stored procedure called "s_res_whats_new". I execute
the stored procedure to return the datareader. I also create my dataset that I will be passing back
to the webservice.