Webservice Design Guidelines

XML Web services is a powerful technology for providing services that can be accessed programmatically across the Internet. The following recommendations will help you create proficient XML Web services:
· XML Web services support both synchronous and asynchronous communication between the client and the server that hosts the XML Web service. Under synchronous communication, the client sends a request for a service to the service host server and waits for the response. This prevents the client from performing other operations while waiting for the results. Asynchronous communication, however, causes the client to continue processing other tasks as it waits for a response. The client responds to the result of the service request when it becomes available.
When you use the Web Services Description Language tool (Wsdl.exe) to create your proxy class, it generates the standard, synchronous versions and asynchronous versions of the methods in the class. The asynchronous versions consist of two methods called Begin and End. The Begin method is used to initiate the XML Web service, while the End method retrieves the results.
Using asynchronous communication improves system usage and avoids delays on the client while it waits for the XML Web service results.
The following code example demonstrates how to make an asynchronous call to an XML Web service from a client application.
[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>





Enter the two numbers you want to add and then press
the Total button.


Number 1:

+
Number 2:

=







For additional information on asynchronous communication, see Communicating with XML Web Services Asynchronously.
· Making numerous service requests across the Internet can affect the performance of the client application. When designing your XML Web service, make efficient use of service requests by creating methods that group related information together. For example, suppose you have an XML Web service that retrieves information about a book. Instead of having separate methods to retrieve the book title, author, and publisher, create a method that returns all this information in one service request. It is more efficient to transfer a large block of information at one time than multiple smaller blocks of information.
The following code example demonstrates how to group related information into a single XML Web service method.
[C#]
<%@ WebService Language="C#" Class="DataService" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
public class DataService {
[WebMethod]
public DataSet GetTitleAuthors() {
SqlConnection myConnection = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;server=localhost;database=pubs");
SqlDataAdapter myCommand1 = new SqlDataAdapter ("select * from Authors", myConnection);
SqlDataAdapter myCommand2 = new SqlDataAdapter("select * from Titles", myConnection);
DataSet ds = new DataSet();
myCommand1.Fill(ds, "Authors");
myCommand2.Fill(ds, "Titles");
return ds;
}
}
· When designing your XML Web service, be sure to use standard object-oriented programming practices. Use encapsulation to hide implementation details. For more complex XML Web services, you can use inheritance and polymorphism to reuse code and simplify your design.
The following code example demonstrates how to use inheritance to create an XML Web service that performs math calculations.
[C#]
<%@ WebService Language="C#" Class="Add" %>
using System;
using System.Web.Services;
abstract public class MathService : WebService
{
[WebMethod]
abstract public float CalculateTotal(float a, float b);
}
public class Add : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a + b;
}
}
public class Subtract : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a - b;
}
}
public class Multiply : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
return a * b;
}
}
public class Divide : MathService
{
[WebMethod]
override public float CalculateTotal(float a, float b)
{
if (b==0)
return -1;
else
return a / b;
}
}
· Use output caching to improve the performance of your XML Web service. When output caching is turned on, the results of a service request are stored in the output cache for a specified duration. If a similar XML Web service request is made, the result can be obtained from the cache, rather than recalculating it. This improves the reaction time of the XML Web service by reducing the processing required by the XML Web service server. Caching can be performed on both the client and the server. The Duration property allows you to specify the amount of time to cache the output of an XML Web service.
The directive to enable output caching on the client is:
<%@ OutputCache Duration="60" %>
The following code example demonstrates how to use the Duration property on the client application to specify output caching for a period of 60 seconds.
[C#]
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>





Enter the two numbers you want to add and press
the Total button.


Number 1:

+
Number 2:

=







You can also use the CacheDuration property of the WebMethod attribute class to enable caching on the server. The following code example demonstrates how to use the CacheDuration property on XML Web service methods to specify output caching for a period of 60 seconds.
[C#]
<%@ WebService Language="C#" Class="MathService" %>
using System;
using System.Web.Services;
public class MathService : WebService {
[WebMethod(CacheDuration=60)]
public float Add(float a, float b)
{
return a + b;
}
[WebMethod(CacheDuration=60)]
public float Subtract(float a, float b)
{
return a - b;
}
[WebMethod(CacheDuration=60)]
public float Multiply(float a, float b)
{
return a * b;
}
[WebMethod(CacheDuration=60)]
public float Divide(float a, float b)
{
if (b==0) return -1;
return a / b;
}
}
· When designing your XML Web service, try to follow the structure of how a schema is formatted.
· XML Web services use SOAP as the primary transport and serialization protocol. A SOAP message consists of an optional set of headers and the message body. The header section contains information that can be processed by the infrastructure on the Web server. SOAP does not define any headers. The body section contains information processed by an application, such as the parameters or return value for an XML Web service.
For additional information on using SOAP headers, see Using SOAP Headers.
· Provide documentation for your XML Web service, such as a static HTML file, that describes the operation of your service and the data structures. Also include examples of how to use the XML Web service. Do not rely on the service description or the service help page as your only documentation.