How to create and consume webservice
In this article am going to explain how to create a webservice using visual studio 2010 and how to consume the created webservice with in the client application.
There might be some other articles too, but I explained clearly step by step which will be use full for beginners to understand easily.
In this article we are going to learn how to create a webservice which has two webmethods and how to consume created service with client application.
Webservice:
Webservice is a class managed by the webserver, which is accessible to client application based on
following standards.
1.WSDL(Webservice Description Language)
2.SOAP(Simple Object Access Protocol)
3.HTTP(Hyper Text Transfer Protocol)
4.UDDI(Universal Description Discovery Intergration)
We are going to use below two methods in our empService
1.getEmp():
This method use to load emp details from access database and stores in the dataset.
2.checkEmp():
This method is used to verify whether the employee exists in the database.
If employee was found then returns the employee record else displays employee does not exist.
Go to access database and create emp table with following records
Empid Ename Egender Esal Edep
------------------------------------------
101 ramu male 2500 1
102 raju male 4500 2
103 rani female 5000 2
104 sridhar male 6000 3
------------------------------------------
Creating webservice using visual studio 2010
-Select new asp.net empty wesite and name as Empwebservice.
-Right click on website path and select add new item.
-Secelct webservice from list and name as empService.
-Visual studio creates asmx and cs files.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Data;
public class empService : System.Web.Services.WebService
{
OleDbConnection cn=new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E://srid documents/mydocsssssss/products.accdb;User ID=admin");
OleDbCommand cmd=new OleDbCommand();
[WebMethod]
public DataSet getEmp(int id)
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from emp where empid=" + id,cn);
DataSet ds = new DataSet();
da.Fill(ds,"p");
return ds;
}
[WebMethod]
public bool checkEmp(int eno)
{
try
{
cmd.CommandText = "select count(*) from emp where empid=" + eno;
cmd.Connection = cn;
cn.Open();
int i = (int)cmd.ExecuteScalar();
if (i==1)
{
return true;
}
else
return false;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cn.Close();
}
}
}
Asmx file of our empService with two webmethods is below
WSDL(Webservice Description Language) file of our empService is below.
Creating client application to consume our empService
-Select new asp.net website, name as clientEmp.
-Add new item, select default.aspx web form.
-Drag and drop gridview control, textbox control and button control.
-Go to solution explorer right click and add webreference.
-Adding reference will create proxy class to communicate with service.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.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)
{
localhost.empService obj=new localhost.empService();
bool b=obj.checkEmp(int.Parse(TextBox1.Text));
if (b == true)
{
System.Data.DataSet ds = obj.getEmp(int.Parse(TextBox1.Text));
GridView1.DataSource= ds.Tables[0];
GridView1.DataBind();
GridView1.Visible = true;
}
else
{
Response.Write("Emp id "+TextBox1.Text+" Doesnot exist");
GridView1.Visible = false;
}
}
}
Run the empService first then run the client application
-If you run directly client application with out running server, you will get following error.
-So make sure that server is running before you run client application. Now press F5 to run client application.
-Enter emp id and click search.
-If emp id doesn't exist out put will be as follows.
Thanks for reading the article.
Hope it is use full for beginners to understand how to create and consume a webservice in asp.net.
Feedbacks and suggestions are welcome.
WSDL:
Web service description language is a xml based language for providing complete description of webservice to client application.
Description contains which data types used, method names and address in webservice.
SOAP:
Simple object access protocol is the formatting protocol which provides standards for sending request and receiving response between client and webservice.
HTTP:
Hyper text transfer protocol is used as a transport to carry request in form of soap message from client to webserver and response from webserver to client.
UDDI:
Universal description discovery and integration is used for describing the services and is platform independent.
Mechanism of HTTP, SOAP in webservice:
1.Client application method call will go to proxy,
proxy will convert method call to xml format that
is soap message.
2.Soap message will reach webserver via HTTP
protocol.
3.Webserver will create object of webservice and
method is executed.
4.The result in the form of soap format is given to
client back over HTTP.
webmethod:
The methods present in webservice which are to be exposed to client application are attributed with webmethod.
Ex: [webmethod]
Public void mymethod()
{
//some logic.
}
Regards
Sridhar.
DNS Member.
"Hope for the best.. Prepare for the worst.."