web
service
A web
service is a web-based functionality accessed using the protocols of the web to
be used by the web applications. There are three aspects of web service
development:
- Creating the web
service
- Creating a proxy
- Consuming the
web service
Creating a Web Service
A web
service is a web application which is basically a class consisting of methods
that could be used by other applications. It also follows a code-behind
architecture such as the ASP.NET web pages, although it does not have a user interface.
To
understand the concept let us create a web service to provide stock price
information. The clients can query about the name and price of a stock based on
the stock symbol. To keep this example simple, the values are hardcoded in a
two-dimensional array. This web service has three methods:
- A default
HelloWorld method
- A GetName Method
- A GetPrice
Method
Take the
following steps to create the web service:
Step (1) :
Select File -> New -> Web Site in Visual Studio, and then select ASP.NET
Web Service.
Step (2) : A
web service file called Service.asmx and its code behind file, Service.cs is
created in the App_Code directory of the project.
Step (3) :
Change the names of the files to StockService.asmx and StockService.cs.
Step (4) :
The .asmx file has simply a WebService directive on it:
<%@ WebService Language="C#" CodeBehind="~/App_Code/StockService.cs" Class="StockService" %>
Step (5) :
Open the StockService.cs file, the code generated in it is the basic Hello
World service. The default web service code behind file looks like the
following:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace StockService
{
// <summary>
// Summary description for
Service1
// <summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// To allow this Web Service
to be called from script,
// using ASP.NET AJAX,
uncomment the following line.
//
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
Step (6) :
Change the code behind file to add the two dimensional array of strings for
stock symbol, name and price and two web methods for getting the stock
information.
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
No comments:
Post a Comment