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; }
- }
- }
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
- {
- // 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]
- 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; }
- }
- }
- }
- 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;
- }
- }
- }
- }
- 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;
- }
- }
- }


Rahul Kumar SaxenaPosted Apr 30, 2016, 1:57 PM
Good Show
Ammar ShaukatPosted Apr 26, 2016, 4:56 AM
why you are adding connection string ?
Sonu ChaudharyPosted Apr 26, 2016, 4:04 AM
good one
Ammar ShaukatPosted Apr 26, 2016, 3:48 AM
You have no idea how write an article..
Ammar ShaukatPosted Apr 26, 2016, 3:36 AM
Debendra Dash
Ammar ShaukatPosted Apr 26, 2016, 3:36 AM
My solution explorer is not showing all files which you specified in your article .
Hari ShankerPosted Apr 25, 2016, 11:27 PM
Thanq ..
Kuppurasu NagarajPosted Apr 25, 2016, 11:57 AM
Nice Sharing