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 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 > 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.
When you will solution explorer, there is already two classes exists [IService1.cs and Service1.cs].
These are created by default when you create new project. So, Delete both.
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
using System.Collections.Generic;
using System.ServiceModel;
namespace MyWcfLibrary
{
[ServiceContract]
public interface IEmployee
{
[OperationContract]
string GetFullName(string firstName, string lastName);
[OperationContract]
List<EmployeeInfo> GetAllEmployeeList();
}
}
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
using System.Runtime.Serialization;
namespace MyWcfLibrary
{
[DataContract]
public class EmployeeInfo
{
[DataMember]
public int EmployeeId { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
[DataMember]
public string Address { get; set; }
}
}
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
using System.Collections.Generic;
namespace MyWcfLibrary
{
public class Employee : IEmployee
{
public string GetFullName(string firstName, string lastName)
{
return firstName + " " + lastName;
}
public List<EmployeeInfo> GetAllEmployeeList()
{
List<EmployeeInfo> employeeList = new List<EmployeeInfo>();
employeeList.Add(new EmployeeInfo() { EmployeeId = 101, FirstName = "Mukesh", LastName = "Kumar", Address = "New Delhi" });
employeeList.Add(new EmployeeInfo() { EmployeeId = 102, FirstName = "Banky", LastName = "Chamber", Address = "Noida" });
return employeeList;
}
}
}
Now, you can run the WCF service library, just press F5. But it will show an error message.
which indicate that the service name is not correct inside the app.config. So, change the service name from Service1 to Employee.
//From
<service name="MyServiceLibrary.Service1">
//To
<service name="MyServiceLibrary.Employee">
Also you can change it for endpoint.
<endpoint address ="" binding="wsHttpBinding" contract="MyWcfLibrary.IEmployee"></endpoint>
Now everything is going fine, press F5 to run the service library.
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 below steps
- On the File menu, choose New, click New Project.
- In the New Project dialog box, expand the Visual C# node and select Windows, and then select Windows Forms Application.
- Give the name of application ClientWindowForm and click on OK.
- Create GUI as below Image for testing the service.
Consuming the WCF Service
For consuming the WCF Service in Window Application, you need to right click on ClientWindowForm and click Add Service Reference. The Add Service Reference dialog box will appear.
In the Add Service Reference dialog box, click Discover.
in above image you can see both method are showing which has 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
using System;
using System.Windows.Forms;
namespace ClientWindowForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnGetName_Click(object sender, EventArgs e)
{
ServiceReference1.EmployeeClient client = new ServiceReference1.EmployeeClient();
var firstName = txtFirstName.Text;
var lastName = txtLastName.Text;
lblFullName.Text = client.GetFullName(firstName, lastName);
}
private void btnGetEmployeeList_Click(object sender, EventArgs e)
{
ServiceReference1.EmployeeClient client = new ServiceReference1.EmployeeClient();
dgvEmployeeList.DataSource = client.GetAllEmployeeList();
}
}
}
For testing when you click on GetName Button after filling the both textbox value with first name and last name. you will get following output.
and when you click on GetEmployeeList button, you will get following result in DataGrid.
So, finally I are able to consume my wcf service in window application.
Conclusion:
So, today I explained how to create a wcf service and consume this service in the window application.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks