Introduction

In this, we tell you why Web Services are an important new development in the area of Internet standards, and what business problems they address.

We talk about the Simple Object Access Protocol (SOAP), which lets you exchange data and documents over the Internet in a well-defined way, and related standards to describe and discover Web Services. Finally, we cover techniques for error handling and state management and discuss how Web Services integrate with the Microsoft .NET platform.

The Role of SOAP

SOAP stands for Simple Object Access Protocol. SOAP was designed with the following three goals in mind:

  1. It should be optimized to run on the Internet.
  2. It should be simple and easy to implement.
  3. It should be based on XML.

SOAP, somewhat contrary to its name, is fundamentally just a protocol that lets two systems: a client and a server exchange data. Of course, the client system may be, and often is, just another server machine, not a human end user.

Creating Your Very First Web Service

Let us look at a SOAP exchange between a client and a server by way of a few examples. Although Web Services are most interesting when used to couple server computers, our examples are more geared towards end users interacting with a Web Service server; we only do this to keep the examples reasonably simple and self contained.

As mentioned earlier, we look only at SOAP as implemented over the HTTP protocol. Also, we initially focus on SOAP as an RPC mechanism. In this, when we discuss the development of a more comprehensive Web Service, you will encounter SOAP used to interchange complex XML documents. Let us start by setting up a simple echo Web Service.This service simply returns whatever character string a user submits.

Creating a class that echoes its input is fairly straightforward, of course

Echo Method
namespace soapExamples
{
     public class simpleService
{
     public simpleService()
{
}
    
public string echo(string input)
{
    
return input;

}}}

How can you now make this into a Web Service? In other words, what is needed to make this method accessible to everybody in the world who has an Internet connection and knows where to find your method?

It may be hard to believe initially, but all that is needed using the .NET Framework is apart from an Internet Information Server (IIS) Web server, of course two tiny little changes:

Your class simpleService needs to inherit from System.Web.Services.WebService.

Your method echo needs to be decorated with the System.Web.Services.WebMethod attribute.

See echo web method for your first fully functioning Web Service.

Echo Web Method (simpleService.asmx.cs)

namespace soapExamples

{

     public class simpleService : System.Web.Services.WebService

{

     public simpleService() {www.syngress.com}

     protected override void Dispose( bool disposing )
{
}

     [System.Web.Services.WebMethod]

     public string echo(string input)
{

     return input;

}

}
}

Let us now open up the Visual Studio.NET integrated development environment and create the echo Web Service from scratch, proceeding as follows:

Create a new ASP.NET Web Service called soapExamples:

Visual Studio.NET Automatically Sets Up a New Web

Creating a New Web Service

Table for Files Created by Visual Studio.NET for soapExamples Web Service