Sei sulla pagina 1di 8

Web service: Web services are for specific job on internet. It is not fully based on internet.

It is capable to use IIS as host. These services are use to perform work remotely on net. Web services are the platform independent. Web service is simply an application that expose a web accessible API. That means you can invoke this application programmatically over the web. Web services allow application to share data using SOAP (simple object access protocol). SOAP is message based protocol on request and response. Credit card validation we can expose as a web service (this is the best example of web service). There are three attributes for the platform independent and all web services follows these attributes. HTTP SOAP XML SOAP=HTML+XML. Html is a stateless protocol that is use for communication. We use XML for data transfer because all application understand XML format. Web service can invoke single method on a stateless object. When we add reference of any class library then a dll copy in the debug folder. If we do local property false then we can use that .dll with full namespace. .vsdisco will contain the reference of web method of web service. .asmx is the web service extension in .net. There is no user interface of web service. We can keep our business logic in web service. Where can we find web service? Web services are registered in the UDDI (universal discovery description and integration) directory. So web service can be found from www.uddi.org. Steps for creating the web service: Step 1: we open the web service in Microsoft visual studio2005. And by default we find a Web method. We can create another method. using using using using System; System.Web; System.Web.Services; System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService

public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; }

Step2: we build and debug service and we get that service as follows:

Step3: After that copy the URL of the service. For Example: http://localhost:1096/service1/Service.asmx?op=sum. Step4: Now we open the asp.net web application. Step5: We add the reference as add web reference.Then new page will be open. Step6: Paste the URL and click on go then we found service after that click on add reference. Step7: AT the last we writen the code of the application. and debug that application. Example: Write a program for sum of two integers with the help of web service. Open the asp.net web service and create a WebMethd as Sum(). Service.cs

using using using using

System; System.Web; System.Web.Services; System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public int sum(int a,int b) { return a+b; } } Build and debug this code and copy the URL. Now open the asp.net application.

Webform.aspx: <%@ 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>Untitled Page</title>

</head> <body> <form id="form1" runat="server"> <div><table cellpadding="0" cellspacing="0" border="2" width="50%" height="250px"> <tr><td align="center" valign="top" style="padding-top:20px;"> <asp:Label ID="Label1" runat="server" Text="Enter the first no."></asp:Label> &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br><br><br> <asp:Label ID="Label2" runat="server" Text="Enter the second no"></asp:Label> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br><br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <asp:Button ID="Button1" runat="server" Text="Sum" OnClick="Button1_Click" /><br><br><br> &nbsp;<asp:Label ID="Label3" runat="server" Text="Result"></asp:Label> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td></tr></table></div> </form> </body> </html>

To add web reference: We add the reference as add web reference. And new page will be open as follows:

Paste the URL and click on go then we found service :

After that click on add reference button then reference will be add. Web form.aspx.cs: Now we add the reference as add web reference. And write the code on the button click as follows using using using using using System; System.Data; System.Configuration; System.Web; System.Web.Security;

using using using using

System.Web.UI; System.Web.UI.WebControls; System.Web.UI.WebControls.WebParts; System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page { localhost.Service obj = new localhost.Service(); protected void Button1_Click(object sender, EventArgs e) { int a, b, c; a = Int32.Parse(TextBox1.Text); b = Int32.Parse(TextBox2.Text); //c = obj.sum(a, b); TextBox3.Text =(obj.sum(a,b)).ToString(); } } Now we debug the web application and we found the following output:

Properties of Webmethod: A web method attributes represents a method for web. Web methods have six properties. These are as follows: Description Message name Enable session Cache duration Transaction option Buffer response Description: [Web service] and [Web method] attribute have a description property. using System;

using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod (Description="this method will add")] public int sum(int a, int b, int c) { return a+b+c; } } Message name: This is useful property, when we want to overload the web method. [WebMethod] public int add(int a, int b, int c) { return a + b + c; } [WebMethod (MessageName="add")] //message public int add(int a) { int s = 0; for (int i = 0; i <= a; i++) s = s + i; return s; } Enable session: This property is used for to enable in the web services. (Remember web services used http protocol this is stateless). if we want to maintain the state between client and server we need to use this property. Default enable session is false. [WebMethod(EnableSession=true)] public string HelloWorld() { return "Hello World"; } Cache duration:

When we cache the previously accessed result next time the same user or different user asks we can serve from cache. Here result cached 60 milliseconds. If we invoke this method within sixty minutes, it will print same time. This will increase the web service performance [WebMethod(CacheDuration=60)] public string showtime() { return DateTime.Now.ToShortTimeString(); } Buffer response: This property is Boolean control web method whether or not to buffer the method's response.

Potrebbero piacerti anche