Creating and consuming WCF Services In asp.net
Creating and consuming WCF Services In asp.net
As we know about Web service, it use for throw your methods
and function to outer world. But web service has limitation in respect to WCF
Services.Webervices is only use for web Application, while we can use WCF
service with web, window, console and any type of application.
So here is our simple example for creating and consuming a
WCf service for web Application.
But you can use this same service with window, console and
any other type of application, by changing its traveling protocols in WEB.Config File.
Step 1: Design Database
Table Creation….
CREATE TABLE [dbo].[GridView](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
[age] [int] NULL,
[salary] [decimal](18, 0) NULL,
[country] [varchar](50) NULL,
[city] [varchar](50) NULL,
CONSTRAINT
[PK_GridView] PRIMARY KEY
CLUSTERED
(
[id] ASC
)WITH
(PAD_INDEX
= OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS
= ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Step 2: Create Wcf Project.
Go to >file>new>Project
And select
WCf Service
Application
Then you will find two main file in you Solution Explorer.
1)
IService1.cs
2)
Service1.svc
In IService1 I am Declared a class and two methods in Interface.
Code is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService_for_inertand_view
{
// NOTE:
You can use the "Rename" command on the "Refactor" menu to
change the interface name "IService1" in both code and config file
together.
[ServiceContract]
public interface IService1
{
[OperationContract]
List<Details> GetDetails(string NAME);
[OperationContract]
string InsertDetails(Details Info);
}
[DataContract]
public class Details
{
string name = string.Empty;
string age = string.Empty;
string salary = string.Empty;
string country = string.Empty;
string city = string.Empty;
[DataMember]
public string Name
{
get { return name; }
set { name = value; }
}
[DataMember]
public string Age
{
get { return age; }
set { age = value; }
}
[DataMember]
public string Salary
{
get { return salary; }
set { salary = value; }
}
[DataMember]
public string Country
{
get { return country; }
set { country = value; }
}
[DataMember]
public string City
{
get { return city; }
set { city
= value; }
}
}
}
In IService1.svc I am Define its methods.
Code is :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfService_for_inertand_view
{
// NOTE:
You can use the "Rename" command on the "Refactor" menu to
change the class name "Service1" in code, svc and config file
together.
// NOTE:
In order to launch WCF Test Client for testing this service, please select
Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
SqlConnection con = new SqlConnection(
"Data
Source=NEWPC-PC;Initial Catalog=Usg;Integrated Security=True");
public List<Details> GetDetails(string NAME)
{
List<Details> info_Details = new List<Details>();
{
con.Open();
SqlCommand cmd = new SqlCommand(
"select * from Gridview where NAME Like
'%'+@Name+'%'", con);
cmd.Parameters.AddWithValue("@Name", NAME);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count;
i++)
{
Details customerInfo = new Details();
customerInfo.Name =
dt.Rows[i]["name"].ToString();
customerInfo.Age =
dt.Rows[i]["age"].ToString();
customerInfo.Salary =
dt.Rows[i]["salary"].ToString();
customerInfo.Country = dt.Rows[i]["country"].ToString();
customerInfo.City =
dt.Rows[i]["country"].ToString();
info_Details.Add(customerInfo);
}
}
con.Close();
}
return info_Details;
}
public string InsertDetails(Details Info)
{
string strMessage = string.Empty;
con.Open();
SqlCommand cmd = new SqlCommand("insert into gridview(name," +
"salary,age,country,city)
values(@Name,@age,@salary,@country,@city)",
con);
cmd.Parameters.AddWithValue("@Name",
Info.Name);
cmd.Parameters.AddWithValue("@age",
Info.Age);
cmd.Parameters.AddWithValue("@salary",
Info.Salary);
cmd.Parameters.AddWithValue("@country",
Info.Country);
cmd.Parameters.AddWithValue("@city",
Info.City);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
strMessage = Info.Name + " inserted successfully";
}
else
{
strMessage = Info.Name + " not inserted successfully";
}
con.Close();
return strMessage;
}
}
}
And Finally Buld it.
If everything come fine then run it and copy its address.
The Address will be like :
Now we are raedy with
our WCF Service with two methods.one GetDetails and other for InsertDetails.
Now we will use this
service in our web application for this we need to create a web application for
consume this service.
Step 3: Create web Application
Go to >file>new>Web Site
Add new web form in your application.
Now first of all Make a
Service Reference In your application.
Steps :
1 Right click on solution explorer. Find Add
Service Reference
2 Now You will
find a window with address Texbox.
Paste your service Address in textbox and then click on GO.
3 : Then you will
find Your service in this widow.select service and click on OK Button.
The service reference will automatically add in your
application.
Step 4:Design Your aspx Page with controls for display and inserting a data using WCF Service.
.Aspx Part :
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2">Wcf Test</td>
</tr>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txt_name" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Age</td>
<td><asp:TextBox ID="txt_age" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Salary</td>
<td><asp:TextBox ID="txt_salary" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Country</td>
<td><asp:TextBox ID="txt_country" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>City</td>
<td><asp:TextBox ID="txt_city" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btn" runat="server" Text="Check Wcf" OnClick="btn_Click" /></td>
</tr>
<tr>
<td colspan="2"> <hr />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gr" runat="server"></asp:GridView>
</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
</div>
</form>
.Cs Part.
Using
Namespace
using ServiceReference1;
//Proxy object for Service
ServiceReference1.Service1Client s = new ServiceReference1.Service1Client();
protected void Page_Load(object sender, EventArgs e)
{
//Here
I am paasing sam as value to service
method.
gr.DataSource= s.GetDetails("sam");
gr.DataBind();
}
protected void btn_Click(object sender, EventArgs e)
{
Details Info = new Details();
Info.Name = txt_name.Text;
Info.Age = txt_age.Text;
Info.Salary = txt_salary.Text;
Info.Country = txt_country.Text;
Info.City = txt_city.Text;
String Msg = s.InsertDetails(Info);
Response.Write(Msg);
}
Finaly It will look
like
This Is the simple example for WCF. But Wcf is so broad. You
can use it according to your need.
Comments
Post a Comment