What is webservice? Create a web service to find area of circle. Also give code to consume it
Web Service CS File using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebServiceSimpleExample : System.Web.Services.WebService { public WebServiceSimpleExample () {} [WebMethod] public double areaOfCircle(double radius) { double area; area = 3.14*radius*radius; return area; } }
Default Page ASPX File: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_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></title> </head> <body> <form id="form1" runat="server"> <div> <b>Calculate Simple Interest using the Web Service</b> <br /> <asp:Label ID="label" runat="server" Text="Radius" Width="100px"></asp:Label> <br /> <asp:TextBox ID="txt" runat="server"></asp:TextBox> <asp:Button ID="btn" runat="server" Text="Calculate Area Of Circle" Width="220px" onclick="btn_Click" /> <br /> <asp:Label ID="result" runat="server" Width="100px"></asp:Label> <br /> </div> </form> </body> </html>
Default CS File : using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e){} protected void btn_Click(object sender, EventArgs e) { localhost.WebServiceSimpleExample ws =
new localhost.WebServiceSimpleExample(); ws.EnableDecompression = true; Double area = 0; if(txtl.Text == "") { result.Text = "Please Enter Radius of Circle"; } else { area = ws.areaOfCircle(System.Convert.ToDouble(txt.Text)); result.Text = area.ToString(); } } }
Web Service Simple Example CS File : <%@ WebService Language="C#" CodeBehind="~/Code/WebServiceSimpleExample.cs"
Class="WebService" %>
Web Config File : <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0"/> </system.web> <appSettings> <add key="localhost.WebServiceSimpleExample"
value="http://localhost:2244/WebServiceSimpleExample/
WebServiceSimpleExample.asmx"/> </appSettings> </configuration>