Step-by-Step Demonstration of Creating and Cosuming WCF services in Visual Studio.

Step-by-Step Demonstration of Creating and Cosuming WCF services in Visual Studio.

This Article demonstrates how to create Windows communication foundation service and consume it in ASP.NET Web application.
Creating the Service
To create a WCF service open Visual Studio
  1. On the File menu, point to New and then click Project.

  2. In the New Project dialog box, expand the Visual C# and click WCF then click WCF Service Library. Click OK to open the project.

 In Solution Explorer, double-click IService1.cs
Here I am going to create a method which will return List of employees and will bind List of Employees to GridView in ASP.NET.
3. Add the below method in IService1.cs
 // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
public interface IService1 {
[OperationContract] List<Employee> GetEmployeesData(); }
4. Right click on the Project Solution, then click Add class to create a DataContract class of Employee.
public class Employee { [DataMember] public int EmployeeID { get; set; } [DataMember] public string FirstName { get; set; } [DataMember] public string LastName { get; set; } [DataMember] public int Age { get; set; } }
5. In solution Explorer double click on Service1.svc and Implement IService1 Interface.
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1 : IService1 { public List<Employee> GetEmployeesData()
   List<Employee> lstEmployees = new List<Employee>(){
                              new Employee{EmployeeID=101,FirstName="Adam", LastName="Daniel",Age=25,},
                              new Employee{EmployeeID=102,FirstName="Kelvin",LastName="D",Age=35},
                              new Employee{EmployeeID =103,FirstName="Kurt",LastName="Delgado",Age=28}
   return lstEmployees;
Testing WCF Service
NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer or set Service1.svc start page and  press F5 to debug. 
6. Double click on the method name, and then Click on Invoke to get the result as per below image
 Consuming WCF Service in ASP.NET Web Application
1. In the Solution Explorer Right click on the Solution and Add New Website
2. Right click on the ASP.NET Website, and Add Service Reference of WCF service as per below Image
Note: Here I have given localhost URL as WCF Service self hosted in the local machine
3. Once Service reference added to the solution, open Web.config of ASP.NET application and you will able to find below configuration of WCF service
<binding name="BasicHttpBinding_IService1" />

<endpoint address="http://localhost:6524/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="EmployeeService.IService1" name="BasicHttpBinding_IService1" />
4. Double click on Default.aspx and add Gridview to bind EmployeeService
<form id="form1" runat="server">
<h2>Binding WCF Service in Gridview</h2>
<div <asp:GridView ID="GridView1" runat="server"></asp:GridView>
5.  Go to Default.aspx.cs page to access EmployeeService.
first add the namespace of Employeeservice
using EmployeeService;
protected void Page_Load(object sender, EventArgs e)
Service1Client employeeSvc = new Service1Client();// Instantiate the Service1client
GridView1.DataSource = employeeSvc.GetEmployeesData(); GridView1.DataBind();
Press F5 to run the application and see the result
I hope this article will help you to learn to create simple WCF service and  consume it in web application. Please share your valuable feedback on this.
comments powered by Disqus

Related Articles