Editor's Note: This is the second of four excerpts from the second edition of O'Reilly's .NET Framework Essentials, 2nd Ed. by Thuan L. Thai and Hoang Lam. The complete series of excerpts covers Web services in the context of the .NET framework. This second part covers what a Web service provider is, and illustrates it with a detailed example.
In this section, we describe how to develop a Web Service, first from the point of view of service providers and then of the consumers. Web Services providers implement Web Services and advertise them so that the clients can discover and make use of the services. Because Web Services run on top of HTTP, there must be a web server application of some sort on the machine that hosts the Web Services. This web server application can be Microsoft Internet Information Services (IIS), Apache, or any other program that can understand and process the HTTP protocol. In our examples, we use Microsoft IIS, since that is the only web server currently supported by .NET.
|
Related Reading
.NET Framework Essentials |
We will be building a Web Service called PubsWS to let consumers get information from the sample Pubs database. All data access will be done through ADO.NET, so read Chapter 5 before attempting the examples.
Creating a Web Service is a three-step process.
<% webservice ... %> directive, as well as the class that provides the Web Service implementation. To the Web Service clients, this asmx file is the entry point to your Web Service. You need to put this in a virtual directory that has the executescripts permission turned on.
Namespace attribute, specifying your own namespace.
The following C# code demonstrates a simple Web Service that exposes four methods to Internet clients. We emphasize "Internet" because anyone that can access this asmx file on the web server can access these methods, as opposed to your COM component, which can be accessed only by COM clients:
<%@ WebService Language="C#" Class="PubsWS.PubsWS" %>
namespace PubsWS
{
using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.Services;
[WebService(Namespace="http://Oreilly/DotNetEssentials/")]
public class PubsWS : WebService
{
private static string m_sConnStr =
"provider=sqloledb;server=(local);database=pubs; Integrated Security=SSPI";
[WebMethod(Description="Returns a DataSet containing all authors.")]
public DataSet GetAuthors( )
{
OleDbDataAdapter oDBAdapter;
DataSet oDS;
oDBAdapter = new OleDbDataAdapter("select * from authors",
m_sConnStr);
oDS = new DataSet( );
oDBAdapter.Fill(oDS, "Authors");
return oDS;
}
[WebMethod]
public DataSet GetAuthor(string sSSN)
{
OleDbDataAdapter oDBAdapter;
DataSet oDS;
oDBAdapter = new OleDbDataAdapter(
"select * from authors where au_id ='"
+ sSSN + "'", m_sConnStr);
oDS = new DataSet( );
oDBAdapter.Fill(oDS, "SelectedAuthor");
return oDS;
}
[WebMethod(MessageName="GetBooksByAuthor",
Description="Find books by author's SSN.")]
public DataSet GetBooks(string sAuthorSSN)
{
OleDbDataAdapter oDBAdapter;
DataSet oDS;
oDBAdapter = new OleDbDataAdapter(
"select * from titles inner join titleauthor on " +
"titles.title_id=titleauthor.title_id " +
"where au_id='" + sAuthorSSN + "'", m_sConnStr);
oDS = new DataSet( );
oDBAdapter.Fill(oDS, "Books");
oDBAdapter = new OleDbDataAdapter("select * from authors " +
"where au_id='" + sAuthorSSN + "'", m_sConnStr);
oDBAdapter.Fill(oDS, "Author");
return oDS;
}
[WebMethod]
public DataSet GetBooks( )
{
OleDbDataAdapter oDBAdapter;
DataSet oDS;
oDBAdapter = new OleDbDataAdapter("select * from titles" ,
m_sConnStr);
oDS = new DataSet( );
oDBAdapter.Fill(oDS, "Books");
return oDS;
}
} // end PubsWS
}
If you are familiar with ASP, you may recognize the usage of the @ symbol in front of keyword WebService. This WebService directive specifies the language of the Web Service so that ASP.NET can compile the Web Service with the correct compiler. This directive also specifies the class that implements the Web Service so it can load the correct class and employ reflection to generate the WSDL for the Web Service.
Because PubsWS also uses ADO.NET's OLE DB provider for its data-access needs, we have to add a reference to System.Data and System.Data.OleDb, in addition to the System, System.Web, and System.Web.Services namespaces.
Class PubsWS inherits from WebService with the colon syntax that should be familiar to C++ or C# developers:
public class PubsWS : WebService
The four methods that are tagged with WebMethod attributes are GetAuthors( ), GetAuthor( ), GetBooks(string), and GetBooks( ). In C#, you can tag public methods with a WebMethod attribute using the []syntax. In VB, you must use <>. For example, in VB, the second method would be declared as:
<WebMethod( )> Public Function GetAuthor(sSSN As String) As DataSet
By adding [WebMethod] in front of your public method, you make the public method callable from any Internet client. What goes on behind the scenes is that your public method is associated with an attribute, which is implemented as a WebMethodAttribute class. WebMethodAttribute has six properties:
true by default.
SOAPAction request header and nested within the soap:Body element. For HTTP GET and HTTP POST, it is the PathInfo portion of the URI (as in http://localhost// PubsWS/PubsWS.asmx/GetBooksByAuthor).
To set up these properties, pass the property name and its value as a name = value pair:
[WebMethod(Description="Returns a DataSet containing all authors.")]
public DataSet GetAuthors( )
You can separate multiple properties with a comma:
[WebMethod(MessageName="GetBooksByAuthor",
Description="Find books by author's SSN.")]
public DataSet GetBooks(string sAuthorSSN)
If you set up your Web Services from scratch, you should also need to provide the configuration file (web.config) in the same directory as your asmx file. This configuration file allows you to control various application settings about the virtual directory. Here, we set the authentication mode to None to make our Web Services development and testing a little easier. When you release your Web Services to the public, you should change this setting to Windows, Forms, or Passport instead of None:
<configuration>
<system.web>
<authentication mode="None" />
</system.web>
</configuration>
The following list shows the different modes of authentication:
After creating the Web Service, you can provide the supporting files to help in the discovery of the service. The static discovery disco file is as follows:
<?xml version="1.0" ?>
<disco:discovery xmlns:disco="http://schemas.xmlsoap.org/disco/"
xmlns:scl="http://schemas.xmlsoap.org/disco/scl/">
<scl:contractRef ref="http://localhost/PubsWS/PubsWS.asmx?WSDL"/>
</disco:discovery>
Next in this series of excerpts, you'll learn Web Services Consumers using the .NET framework.
View catalog information for .NET Framework Essentials, 2nd Ed.
Return to the .NET DevCenter.
Copyright © 2009 O'Reilly Media, Inc.