Consuming WCF Service in Window Application Client

In this article, I will explain you how to create a WCF service using “WCF Service Library” and also how to host it and consume it in our client application.

When you try to create a new service from new project window in Visual Studio. You will find two main categories for WCF service. One is WCF service library and the second one is WCF service application. If you want to know the difference between WCF Service Application and WCF Service Library, you can visit this link.

Create New WCF Service Library

Open Visual Studio 2012 or any upper version from Visual Studio 2008.

Click on Menu - File, New, then Project. You will get a new project window. Inside the installed template, choose WCF and from the right pane choose “WCF Service Library” project and click on OK.

wcf service library

When you will visit solution explorer, already tow classes exist: [IService1.cs and Service1.cs].

solution explorer
These are created by default when you create new project, so delete both of them.

And add new interface named with “IEmployee.cs” from the right click on project file and add [ServiceContract] attribute for it which exists in System.ServiceModel namespace. After that add some member function with [OperationContract] attribute.

IEmployee.cs

  1. using System.Collections.Generic;  
  2. using System.ServiceModel;  
  3. namespace MyWcfLibrary  
  4. {  
  5.     [ServiceContract]  
  6.     public interface IEmployee  
  7.     {  
  8.         [OperationContract]  
  9.         string GetFullName(string firstName, string lastName);  
  10.         [OperationContract]  
  11.         List < EmployeeInfo > GetAllEmployeeList();  
  12.     }  
  13. }  
Next add a new class “EmployeeInfo.cs” with [DataContract] attribute for the class which exists System.Runtime.Serialization namespace. Here I will create the property for Employee.

EmployeeInfo.cs
  1. using System.Runtime.Serialization;  
  2. namespace MyWcfLibrary  
  3. {  
  4.     [DataContract]  
  5.     public class EmployeeInfo  
  6.     {  
  7.         [DataMember]  
  8.         public int EmployeeId  
  9.         {  
  10.             get;  
  11.             set;  
  12.         }  
  13.         [DataMember]  
  14.         public string FirstName  
  15.         {  
  16.             get;  
  17.             set;  
  18.         }  
  19.         [DataMember]  
  20.         public string LastName  
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.         [DataMember]  
  26.         public string Address  
  27.         {  
  28.             get;  
  29.             set;  
  30.         }  
  31.     }  
  32. }  
Finally you need to add a class which implements the contract “IEmployee.cs”. So, I am going to add a new class “Employee.cs” which implement the “IEmployee.cs”. In this class, I will implement the method of IEmployee.cs.

Employee.cs
  1. using System.Collections.Generic;  
  2. namespace MyWcfLibrary  
  3. {  
  4.     public class Employee: IEmployee  
  5.     {  
  6.         public string GetFullName(string firstName, string lastName)  
  7.         {  
  8.             return firstName + " " + lastName;  
  9.         }  
  10.         public List < EmployeeInfo > GetAllEmployeeList()  
  11.         {  
  12.             List < EmployeeInfo > employeeList = new List < EmployeeInfo > ();  
  13.             employeeList.Add(new EmployeeInfo()  
  14.             {  
  15.                 EmployeeId = 101, FirstName = "Mukesh", LastName = "Kumar", Address = "New Delhi"  
  16.             });  
  17.             employeeList.Add(new EmployeeInfo()  
  18.             {  
  19.                 EmployeeId = 102, FirstName = "Banky", LastName = "Chamber", Address = "Noida"  
  20.             });  
  21.             return employeeList;  
  22.         }  
  23.     }  
  24. }  
Now, you can run the WCF service library, just press F5. But it will show an error message.

error

That indicate the service name is not correct inside the app.config. So, change the service name from Service1 to Employee.
  1. //From  
  2. < service name = "MyServiceLibrary.Service1" >  
  3.     //To   
  4.     < service name = "MyServiceLibrary.Employee" >  
Also you can change it for endpoint.
  1. <endpoint address="" binding="wsHttpBinding" contract="MyWcfLibrary.IEmployee"></endpoint>  
Now everything is going fine, press F5 to run the service library.

wcf test

Creating Client Window Application

Here I am going to use a Window Application as a client which consume the WCF Service which is already created. So, for creating the window application, follow the below given steps,

 

  1. On the File menu, choose New, then click New Project.
  2. In the New Project dialog box, expand the Visual C# node and select Windows, and then select Windows Form Application.
  3. Give the name of application ClientWindowForm and click OK.
  4. Create GUI as in the following Image for testing the service.

create gui

Consuming the WCF Service

For consuming the WCF Service in Window Application, you need to right click on ClientWindowFormand click Add Service Reference. The Add Service Reference dialog box will appear.

In the Add Service Reference dialog box, click Discover.

Discover

In above image you can see both the methods are showing created in WCF service. Now Click OK to add the service reference. So, finally reference of WCF service has been added in window application. to use this service we need to create the instance of service client.

Make changes in Form1.cs to use this service.

Form1.cs

  1. using System;  
  2. using System.Windows.Forms;  
  3. namespace ClientWindowForm  
  4. {  
  5.     public partial class Form1: Form  
  6.     {  
  7.         public Form1()  
  8.         {  
  9.             InitializeComponent();  
  10.         }  
  11.         private void btnGetName_Click(object sender, EventArgs e)  
  12.         {  
  13.             ServiceReference1.EmployeeClient client = new ServiceReference1.EmployeeClient();  
  14.             var firstName = txtFirstName.Text;  
  15.             var lastName = txtLastName.Text;  
  16.             lblFullName.Text = client.GetFullName(firstName, lastName);  
  17.         }  
  18.         private void btnGetEmployeeList_Click(object sender, EventArgs e)  
  19.         {  
  20.             ServiceReference1.EmployeeClient client = new ServiceReference1.EmployeeClient();  
  21.             dgvEmployeeList.DataSource = client.GetAllEmployeeList();  
  22.         }  
  23.     }  
  24. }  
For testing when you click on GetName button after filling both the textbox value with first name and last name you will get the following output.

GetName

and when you click on GetEmployeeList button, you will get following result in DataGrid.

GetEmployeeList

So, finally I am able to consume my WCF service in windows application.

So, today I explained how to create a WCF service and consume this service in the windows application.