Web Service represent the business logic encapsulated in a
component. Since these services are hosted on a http server these are referred
as web services.
Web Services are stateless in nature. i.e. Every request
treated as a new client.
The request and response data is transmitted using XML.
Hence, they are architecture neutral. i.e. A web service developed using
JAVA(JSP) may be consumed by an ASP.NET web page.
The underlying protocol used is SOAP(Simple object access
protocol) SOAP envelope consist of SOAP Header and SOAP Body.
Note:
A WebService class is derived from
System.Web.Services.WebService
The attribute [WebService]
marked for the class indicates that the class will act as a web service.
The web service class may consist of several methods but only
those methods which are marked by the attribute [WebMethod] will be exposed to
the web service consumer.
A Consumer is the web application that will send request to
the web service and obtain a response.
The attribute [WebService] has a property Namespace whose
ideal value may be URL of the web service where it is hosted conventionally,
otherwise it may be set to any non-existent URL.
Example:
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)]
// 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 Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using
designed components
//InitializeComponent();
}
[WebMethod]
public int Addition(int x,int y)
{
return x + y;
}
}
Consume a
Web Service into ASP.NET Web Application
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 Button1_Click(object sender,
EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);
int b = Convert.ToInt32(TextBox2.Text);
MyWebservice.Service affy = new
MyWebservice.Service();
int c = affy.Addition(a, b);
TextBox3.Text = c.ToString();
}
}
For Complete ASP.NET Training in Gwalior

No comments:
Post a Comment