Below is Sample example of web service in asp c# .net :
1. File--->New Project----->Select template Web (ASP .NET Web Application)---->Name of the Application .
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalculatorWebApplication.WebForm1" %>
After view service shows below screen.
Ans: Since these are open and well known protocols ,applications built on any platform can interoperate with web services.
Example: A java application can interoperate with a web service built using .net similarly a web service built using java can be consumed by a .net application.
Out Put:
1. File--->New Project----->Select template Web (ASP .NET Web Application)---->Name of the Application .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebServicesDemo
{
/// <summary>
/// Summary description for CalculatorWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.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 CalculatorWebService : System.Web.Services.WebService
{
[WebMethod]
public int Add(int firstValue,int secondValue)
{
return firstValue + secondValue;
}
//public
string HelloWorld()
//{
// return "Hello World";
//}
}
}
2 . Add new web application then add web service reference show below image.
New Added Web Application --->Refernce---> Add Service Refernce .
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalculatorWebApplication.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table style="font-family:Arial;align-items:center ">
<tr>
<td>
<b>First Value</b>
</td>
<td>
<asp:TextBox ID="txtFirstValue" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Second Value</b>
</td>
<td>
<asp:TextBox ID="txtSecondValue" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Result</b>
</td>
<td>
<asp:Label ID="lblResult" runat="server" ></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CalculatorWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
CalculatorService.CalculatorWebServiceSoapClient client = new CalculatorService.CalculatorWebServiceSoapClient();
int result = client.Add(Convert.ToInt32(txtFirstValue.Text), Convert.ToInt32(txtSecondValue.Text));
lblResult.Text = result.ToString();
}
}
}
Web services use open standards and protocols like HTTP,XML and SOAP.
No comments:
Post a Comment