The three major components that make up a Web Service are as
follows.
·
A Web Service that runs on the Web Server
·
A Client application that invokes the Web Service
·
A WSDL document that describes the Web Service and specifies how
to discover the Web Service
Web Services can be implemented in .NET in a couple of ways.
We can have self contained services that use a single .ASMX page containing
both the Web Service header and the source code for the Web Service. The second
way is to have a Web Service header separately and reference a Web Service class
externally. The following code sample shows how the former can be implemented.
Listing 3
<%@ WebService Language="C#" Class="TestWebService" %>
using System;
using System.Web;
using System.Web.Services;
public class TestWebService
{
[WebMethod]
public string Display()
{
return "This is a Web Method";
}
}
Note the header at the top of the code in the listing above;
it specifies the name of the class and the language to be used. The following
code snippet shows how we can implement the later approach, i.e., the Web
Service class is referenced externally here.
Listing 4
<%@ WebService Language="c#"
Codebehind="TestWebService.asmx.cs" Class="AspAlliance.TestWebService" %>
To test the Web Service, navigate to http://localhost/TestWebService/Service.asmx.
To access this Web Service from a Client application, add
the Web Service as a web reference to the Client Application and call its methods
as shown below.
Listing 5
Service s = new Service();
MessageBox.Show(s.Display());