What is WCF?
In simple terms WCF can be thought of as the next generation of webservices. In true sense however, WCF is actually a replacement for both webservices and remoting.
The image below shows you a real life scenario where a WCF service is used to authorize a user access to a website within the LAN of a corporation. This WCF service has a method that takes the logged in user's LAN ID as an input parameter and returns a boolean value which is true if the user's designation is Manager or above. And it returns false if the user's designation is below that of a manager.
In this article I will show you a quick and easy way to build a WCF service and a WCF client that accesses that service.
Building a WCF Service
Building a WCF service mainly involves 3 steps
- Defining the service contract : This defines the signatures of all the methods provided by the service
- Implementing the service contract: This code implements the methods defined in the service contract.
- Hosting the service
A WCF service can be hosted in four different ways
1>As a console application
2>As a website in IIS
3>As a Windows service
4>As a Windows Activation Service
In the example below I have built the WCF service a console application. In this example, defining and implementing the service is super simple. Hosting the service is the only part that will take slightly longer to understand.
To build a WCF service as a console application, this is what you would do
1>Create a console application project and call it SimpleService
2> Add a reference to System.ServiceModel
3>Once you do that it will appear in your references folder
4> Add a new Class file to the project where you would define your service contract. What this means is that this code defines the signatures of the methods that this service will expose. Name the class file as DefineServiceContract.cs
5>Paste the code below in that page.
public interface IDilbertFunctions
/* Here you define the service contract.
In other words, this is where you specify
what functions (web methods) your service will provide
double Add(double x, double y);
6>Add another class file called ImplementServiceContract.cs .In this page you have to implement the methods defined in your service contract. Paste the code below do do that.
class DilbertFunctions : IDilbertFunctions
//This is the class where you implement the web methods defined in the service contract.
public string HelloWorld()
return "Hello World";
public double Add(double a, double b)
double sum = (a + b);
7>Host the service by pasting the code below to the program.cs file
static void Main(string args)
//This is where you host the service
Uri baseAddress = new Uri("http://localhost:8000/DilbertService");//"DilbertService can be replaced with any name you want.
ServiceHost selfHost = new ServiceHost(typeof(DilbertFunctions), baseAddress);//DilbertFunctions is the name of the class that implements the web methods
selfHost.AddServiceEndpoint(typeof(IDilbertFunctions), new WSHttpBinding(), "DilbertFunctions");//IDilbertFunctions is the name of the interface that defines the service contract
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
// Close the ServiceHostBase to shutdown the service.
9>Run the service by pressing ctrl+f5 . After which you will see a screen as shown below
10> Now if you paste this url http://localhost:8000/DilbertService into internet explorer, you should be able to see this
How to test this service using WCF test client
How to consume this service by creating a WCF client
Windows Communication Foundation .net 4.0: