The .NET Framework makes creating and consuming XML Web Services easy, allowing developers to bring added functionality to their applications and services almost overnight. This tutorial will provide you with an introduction to Web Services and .NET, and show you how to create a Web Service which retrieves information from a database (Part 2 will show you how to consume the service through an ASP.NET page).
First a quick overview on Web Services and .NET:
- A Web Service is a program (or programmable unit) which can be accessed over the Internet from disparate systems using standard Internet Protocols like HTTP (GET and POST) or SOAP.
- By serving as a framework for XML Web Services in managed code, the ASP.NET page framework allows developers to utilize many features from the .NET Framework like session state.
- Web Services use ".asmx" files in the ASP.NET application model and serve as the addressable entry point for a XML Web Service which references code in pre-compiled assemblies, code-behind files, or the ".asmx" file.
- XML Web services should be accessible no matter what operating system, object model or programming language is being used to access the service.
- They should be loosely coupled and use a universal data format.
- Can use WSDL files for discovery of the service.
- Should provide a service description to define its methods and how to interact with the Web Service.
What's great about .NET is that it automates many of these tasks including creating an entry point with the service name and description, a link to the formal description (WSDL), as well as a list and description of the services operations. In addition it also creates help pages for each of the operations allowing a user to test the Web Service as well as providing information on how to interact with the operation via SOAP, HTTP-GET and HTTP-POST. And it creates all of this from our one ".asmx" file.
So let's start with our ".asmx" file and the code needed to produce our Web Service called MyDataService which has one method/operation GetData which returns data from our database.
xmlwebservice_data.asmx
<%@ WebService Language="c#" Class="MyDataService" %>
using System;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
[WebService(Namespace="http://myserver/MyDataService/",
Description="A Web Service To Retrieve Data From A Database",
Name="MyDataService")]
public class MyDataService : WebService
{
[WebMethod(BufferResponse=true,
CacheDuration=0,
Description="This method retrieves all data from our database",
MessageName="GetData")]
public DataSet GetData()
{
String DataSource = "server=yoursqlserver;database=yourdb;uid=user;pwd=pass;";
SqlConnection conn = new SqlConnection(DataSource);
String sql = "Select * FROM Articles Order By ArticleDate ASC";
conn.Open();
SqlDataAdapter db_sqladaptor = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
db_sqladaptor.Fill(ds,"MyDataResult");
return ds;
}
}
|
Much of the code should look familiar if you have already worked with code-behind files. Skipping over the Web Service code for now, we're just importing our namespaces to retrieve SQL data, setting our class name, and creating a method which returns a DataSet (make sure to replace the sql and namespace information to suit your specific sql data to retrieve).
When creating a Web Service you need to include a reference to the namespace System.Web.Services as it should derive from the base WebService class.
At the top of your ".asmx" file you need to specify the page as a Web Service with the
WebService Processing Directive, which correlates the URL of the XML Web service with the service's implementation, which takes the following attributes:
- Language - the language your service is created in.
- Code-Behind - a reference to a code-behind file/assembly if used.
- Class - the class used in the ".asmx" file, or the class in the code-behind file which implements the Web Service functionality.
Before you specify your class in your ".asmx" file you need to specify the WebService Attribute which can specify either all, some, or none of the properties:
- Description - the description of your service.
- Name - the name of your service. If left blank it will use the name of the class in your processing directive.
- Namespace - the namespace of your service. If left blank it will use the default namespace of http://tempuri.org. However this should be changed to your namespace (server) before going public. A XML namespace is simply a set of distinct characters to differenciate your service and methods. A good rule of thumb is to use the url (or directory) of your Web Service since most likely the characters that comprise the url is unique.