May 13, 2021 ASP.NET
A Web service is a web-based feature that can be obtained by a web app through a web network protocol. W eb service development consists of the following three main areas:
A web service is a web app that basically consists of a class that contains multiple methods that can be called by other apps, and it also uses a hidden code structure such as an ASP.NET web page, but it does not have a user interface.
To better understand this concept, let's create a web service that provides stock price information. C lients of the service can query the relevant name and price through the stock label. T o simplify this example, we set the stock price to a fixed value and save it in a two-dimensional list. This web service consists of three methods:
Take the following steps to create the service:
Step (1): Select File -gt; New - Web Site in Visual Studio, and then select ASP.NET Web Service.
Step (2): A web service file named Service.asmx and its code are hidden, and service.cs is created under the App_Code path of the project.
Step (3): Change the file name to StockService.asmx and StockService .cs.
Step (4): The .asmx file simplifies a WebService directive as follows:
<%@ WebService Language="C#" CodeBehind="~/App_Code/StockService.cs"
Class="StockService" %>
Step (5): Open the StockService .cs file, which generates the code that is the underlying code for the Hello World service. T he default web service code is as follows:
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): Modify the code in the file to add a two-dimensional pointer to a string that stores the names and prices of each stock, and write two web methods to get stock information as follows;
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)]
// 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 StockService : System.Web.Services.WebService
{
public StockService () {
//Uncomment the following if using designed components
//InitializeComponent();
}
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];
}
return "Stock Not Found";
}
}
Step (7): Running the web service app gives a web service test page on which we can test the service method.
Step (8): Click on a method name to confirm that it is running correctly.
Step (9): To detect the GetName method, provide one of the stock tags that has been defined and, correctly, return the name of the relevant stock.
To use the web service, we create a Web site under the same solution, just right-click the solution name on Solution Manager, and the web page called by the web service should have a control management to display the returned results and two control buttons, one for returning another for starting the call to the service.
The web app's files are as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="wsclient._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
Untitled Page
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Using the Stock Service</h3>
<br /> <br />
<asp:Label ID="lblmessage" runat="server"></asp:Label>
<br /> <br />
<asp:Button ID="btnpostback" runat="server" onclick="Button1_Click" Text="Post Back" style="width:132px" />
<asp:Button ID="btnservice" runat="server" onclick="btnservice_Click" Text="Get Stock" style="width:99px" />
</div>
</form>
</body>
</html>
The code for the web app is as follows:
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;
//this is the proxy
using localhost;
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();
}
}
protected void btnservice_Click(object sender, EventArgs e)
{
StockService proxy = new StockService();
lblmessage.Text = String.Format("Current SATYAM Price:{0}",
proxy.GetPrice("SATYAM").ToString());
}
}
}
A proxy server is a substitute for a web service code. B efore we can use the web service, we must create a proxy server. T his proxy server is registered by the client app. T he client app then implements a call to the web service to make it look like it's using a local method.
The proxy server will call and send the call to the server in the appropriate format as if it were a SOAP request. S OAP supports Simple Object Access Protocol. This protocol applies to web service data exchange.
When this server responds and returns a SOAP package to the client, the proxy server presents everything to the client application.
Web btnservice_click be added to your application before calling a Web service using the web app. This transparently creates a proxy class that can be used by btnservice_click events.
protected void btnservice_Click(object sender, EventArgs e)
{
StockService proxy = new StockService();
lblmessage.Text = String.Format("Current SATYAM Price: {0}",
proxy.GetPrice("SATYAM").ToString());
}
Take these steps to create a proxy:
Step (1): Right-click 'Add Web Reference' at the entrance to the solution Explorer's web app.
Step (2): Selecting 'Web Services in this solution' returns the stock service reference we wrote.
Step (3): Click on the service to open the test page and create the proxy by default as 'localhost', which you can rename. C lick 'Add Reference' to add a proxy to the client application.
Add the following statement to your code to include the agent:
using localhost;