Asp Tutorial Notes and Web Services PDF
Asp Tutorial Notes and Web Services PDF
Asp Tutorial Notes and Web Services PDF
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:
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:
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 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)]
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;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
string[,] stocks =
{
{"RELIND", "Reliance Industries", "1060.15"},
{"ICICI", "ICICI Bank", "911.55"},
{"JSW", "JSW Steel", "1201.25"},
{"WIPRO", "Wipro Limited", "1194.65"},
{"SATYAM", "Satyam Computers", "91.10"}
};
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public double GetPrice(string symbol)
{
//it takes the symbol as parameter and returns price
for (int i = 0; i < stocks.GetLength(0); i++)
{
if (String.Compare(symbol, stocks[i, 0], true) == 0)
return Convert.ToDouble(stocks[i, 2]);
}
return 0;
}
[WebMethod]
public string GetName(string symbol)
{
// It takes the symbol as parameter and
// returns name of the stock
for (int i = 0; i < stocks.GetLength(0); i++)
{
if (String.Compare(symbol, stocks[i, 0], true) == 0)
return stocks[i, 1];
}
Step 7 : Running the web service application gives a web service test page, which allows testing
the service methods.
Step 9 : For testing the GetName method, provide one of the stock symbols, which are hard coded,
it returns the name of the stock
<head runat="server">
<title>
Untitled Page
</title>
</head>
<body>
<form >
<div>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace wsclient
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblmessage.Text = "First Loading Time: " + DateTime.Now.ToLongTimeString
}
else
{
lblmessage.Text = "PostBack at: " + DateTime.Now.ToLongTimeString();
}
}
The proxy takes the calls, wraps it in proper format and sends it as a SOAP request to the server.
SOAP stands for Simple Object Access Protocol. This protocol is used for exchanging web service
data.
When the server returns the SOAP package to the client, the proxy decodes everything and
presents it to the client application.
Before calling the web service using the btnservice_Click, a web reference should be added to the
application. This creates a proxy class transparently, which is used by the btnservice_Click event.
Step 1 : Right click on the web application entry in the Solution Explorer and click on 'Add Web
Reference'.
Step 2 : Select 'Web Services in this solution'. It returns the StockService reference.
Step 3 : Clicking on the service opens the test web page. By default the proxy created is called
'localhost', you can rename it. Click on 'Add Reference' to add the proxy to the client application.
using localhost;
Loading [MathJax]/jax/output/HTML-CSS/jax.js