Creating WCF Services
Before Creating any
WCF service I want to give a brief description about WCF. WCF is a Microsoft technology provided in DotNet Framework for development rich distributed application.
In Visual studio 2008 the first version of WCF was introduced. Before WCF we were mainly using "WEB SERVICES" for communication with diffrent platform applications. Introduction of WCF brings a great revolution in "SOA"(Service Oriented Archicture).
Here I will explain in detail how to create and consume WCF Service in your application. First for Creating WCF service open Visual studio and follow the given steps.
Create a new project of type(WCF) and choose the following template as shown below.
Now the following things get added into your project in solution Explorer.
Now I am adding one model class called "Employee.cs" to my project as follow.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace WCF_REST
- {
- public class Employee1
- {
- public int EmpId { get; set; }
- public string EmpName { get; set; }
- public string CompanyName { get; set; }
- public string Location { get; set; }
- public string Dept { get; set; }
-
- }
- }
After that I will add a "WCFDAL.cs" class to perform database operations in my service. Here my solution explorer looks like this.
Now open the Web.config and write the following connection string in it.
- <connectionStrings>
- <add name="connect" connectionString="Server=Debendra; Database=students; User ID=sa;Password=123;" />
-
- </connectionStrings>
Now open the IService1.cs and define the following interfaces for operation.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Text;
- using System.Data;
-
- namespace WCF_REST
- {
-
- [ServiceContract]
- public interface IService1
- {
-
- [OperationContract]
- bool InsertData(Employee1 obj);
- [OperationContract(Name = "ShowAll")]
- [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/ShowAll")]
- List<Employee1> ShowAll();
- [OperationContract]
- [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/showdata/")]
- List<Employee1> getRecordbyId(int id);
- [OperationContract]
- bool UpdateData(Employee1 obj);
- [OperationContract]
- bool DeleteData(Employee1 obj);
-
-
- }
- [DataContract]
- public class Employee
- {
-
- string _name = "";
- string _email = "";
- string _phone = "";
- string _gender = "";
- [DataMember]
- public int EmpId
- {
- get { return EmpId; }
- set { EmpId = value; }
- }
-
- [DataMember]
- public string EmpName
- {
- get { return _name; }
- set { _name = value; }
- }
-
- [DataMember]
- public string CompanyName
- {
- get { return _email; }
- set { _email = value; }
- }
-
- [DataMember]
- public string Location
- {
- get { return _phone; }
- set { _phone = value; }
- }
-
- [DataMember]
- public string Dept
- {
- get { return _gender; }
- set { _gender = value; }
- }
- }
- }
Now here is the "Service1.svc.cs" class to implement all these interfaces.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ServiceModel;
- using System.ServiceModel.Web;
- using System.Data.SqlClient;
- using System.Data;
- using System.Configuration;
-
-
- namespace WCF_REST
- {
- public class Service1 : IService1
- {
- WCFDAL myobject = new WCFDAL();
- public bool InsertData(Employee1 obj)
- {
- string query = "insert into Employee(EmpName,CompanyName,Location,Dept) values('" + obj.EmpName + "','" + obj.CompanyName + "','" + obj.Location + "','" + obj.Dept + "')";
- bool x = myobject.DML(query);
- return x;
-
-
-
- }
- public bool UpdateData(Employee1 obj)
- {
- string query = "update Employee set EmpName='" + obj.EmpName + "',CompanyName='" + obj.CompanyName + "',Location='" + obj.Location + "',Dept='" + obj.Dept + "' where Empid='" + obj.EmpId + "' ";
- bool x = myobject.DML(query);
- return x;
-
-
- }
- public bool DeleteData(Employee1 obj)
- {
- string query = "Delete from Employee where Empid='" + obj.EmpId + "'";
- bool x = myobject.DML(query);
- return x;
-
- }
-
- public List<Employee1> ShowAll()
- {
- List<Employee1> li = new List<Employee1>();
- string s = "select * from Employee";
- DataTable dt = new DataTable();
- dt = myobject.getdata(s);
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- Employee1 emp = new Employee1();
- emp.EmpId = Convert.ToInt32(dt.Rows[i]["EmpId"]);
- emp.EmpName = dt.Rows[i]["EmpName"].ToString();
- emp.CompanyName = dt.Rows[i]["CompanyName"].ToString();
- emp.Dept = dt.Rows[i]["Dept"].ToString();
- emp.Location = dt.Rows[i]["Location"].ToString();
-
-
- li.Add(emp);
-
- }
-
-
- return li;
-
-
-
- }
- public List<Employee1> getRecordbyId(int id)
- {
- List<Employee1> li = new List<Employee1>();
- DataTable dt1 = new DataTable();
- string query="select * from Employee where EmpId='"+id+"'";
- dt1= myobject.getdata(query);
- if(dt1.Rows.Count>0)
- {
- for (int i = 0; i < dt1.Rows.Count; i++)
- {
- Employee1 emp = new Employee1();
- emp.EmpId = Convert.ToInt32(dt1.Rows[i]["EmpId"]);
- emp.EmpName = dt1.Rows[i]["EmpName"].ToString();
- emp.CompanyName = dt1.Rows[i]["CompanyName"].ToString();
- emp.Dept = dt1.Rows[i]["Dept"].ToString();
- emp.Location = dt1.Rows[i]["Location"].ToString();
-
-
- li.Add(emp);
-
- }
- return li;
-
- }
- else
- {
- return li;
- }
-
-
- }
-
- }
- }
Here for Database operation I am calling to "WCFDAL .cs
" here as code for this class.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
-
- namespace WCF_REST
- {
- public class WCFDAL
- {
- SqlCommand cmd;
- SqlDataAdapter da;
- DataSet ds;
- public static SqlConnection connection()
- {
- string s = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
- SqlConnection con = new SqlConnection(s);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- else
- {
- con.Open();
- }
- return con;
- }
- public bool DML(string Query)
- {
- cmd = new SqlCommand(Query, WCFDAL.connection());
- int x = cmd.ExecuteNonQuery();
- if(x==1)
- {
- return true;
- }
- else
- {
- return false;
- }
-
-
-
- }
- public DataTable getdata(string query)
- {
- da = new SqlDataAdapter(query, WCFDAL.connection());
- DataTable dt = new DataTable();
- da.Fill(dt);
- return dt;
-
-
- }
-
-
- }
- }
Now that's all from WCF service side to check this service we need the TEST Client. Now Save and Run this project. It will open the test Client as follows.
Now here you can test your individual service seperatly.
Consuming WCF Service in ASP DOTNET Application :
Now to Consume this service add a new project in this Solution Explorer Name it as "WCFClient" and add a page(CRUD.aspx) as follow.
Now Add the Service Referance as follow.
Now design the CRUD.aspx as follow to perform CRUD operation.
Here is the code for the design page.
Now write the following logic in the code behind window.
After this you can run and check your project .
It will get added like this.
Now if you want to Edit you can Edit Like this.
Now after Clicking Update it will update it as follows.
So you can update anything by clicking update button.Similarly the delete button will work and delete the record you selected.
So in this way we can create WCF service and Consume in our Asp.Net Application. You can check the
project sample on gitHub.
Read more articles on ASP.NET: