Understand WCF: Part-3: Create Simple WCF Service

In this article, we will concentrate directly to WCF and we will create a sample WCF application.

Welcome to the Understand WCF article series. The previous two articles explained a few theoretical parts of WCF and related to WCF services. We have learned what SOA, Messages, Services and properties of RESTful services are. You can read them here:
In this article, we will concentrate directly on WCF and we will create a sample WCF application. As we know, WCF is nothing but service and we need to create a client to consume the service. Here we will create a small console application to consume the service. So, let's start to create the example.
 

Step 1: Create WCF service

 
This is the first step towards implementation. Here we will create a WCF service to convert temperature. This service will convert a temperature from Farenhight to Celsius. Let's open Visual Studio and open one WCF template. Once you have opened the WCF template you will find one interface and one class that has implemented the interface.
 
Modify the interface by the following code.
 
Here, we need to configure the interface at first. Let's modify the default interface as below. The ServiceContract attribute ensures that this interface will be exposed to the outside world. And we have specified the GetTemperature() method as OperationContract that will ensure that it is a service method and from the outside world people will be able to call this method.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace NameService  
  9. {  
  10.     [ServiceContract]  
  11.     public interface ITemperatureService  
  12.     {  
  13.         [OperationContract]  
  14.         string GetTemperature(int Faren);  
  15.     }  
  16. }  
Implement the interface
 
Now, we need to implement this interface in an actual class. Here is the class structure.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7. namespace NameService  
  8. {  
  9.     public class TemperatureService : ITemperatureService  
  10.     {  
  11.         public string GetTemperature(int Faren)  
  12.         {  
  13.             return Convert.ToString(((Faren - 32) / 9) * 5);  
  14.         }  
  15.     }  
  16. }  
We see that the getTemperature() method is getting the temperature as Farenhight and it will return the corresponding Celsius value of the temperature.
 
We can test the service application to check whether it's working perfectly or not. If we run the .svc file of the service application then we will find the following output in the browser. It ensures that our service is running and that the external world can consume it.
 
WCF Service URL 
 
Even without creating any client we can invoke the service from a console of a WCF service. Let's run the service application. You will see below the pop-up window that gets input for the service. Once you provide a value and press the "invoke" button, you will find that the service returns a value.
 
In my case I have specified Farenhight value 100 and the service returned the corresponding Celsius value 35.
 
WCF Service Returning Value 
 

Step 2: Create client to consume the service

 
We will create a simple console application to consume the service. Let's create a simple console application. At first we need to provide a service reference.
 
Right-click on the console application and select "Add Service Reference". You will see a dialogue box to find the service. Click on the "Discover" button and it will show our previously created service as in the following.
 
Add service reference 
 
Once you press "OK", the reference will be added to our console application. Adding the reference is necessary to create a proxy and this proxy we will consume the web service. Once you have successfully added a reference you will find the reference has been added to the console supplication as in the following.
 
WCF service reference 
 
Now we will call the service class to consume the service. Have a look at the following code.
  1. using System;  
  2. using System.Text;  
  3. using Client.TemperatureService;  
  4. namespace Client  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             TemperatureService.TemperatureServiceClient obj = new TemperatureServiceClient();  
  11.             Console.WriteLine("Value is :- " + obj.GetTemperature(100));  
  12.             Console.ReadLine();  
  13.         }  
  14.     }  
  15. }  
Don't forget to include the namespace of the WCF service application.
 
Here is sample output:
 
WCF Service Output 
 
Conclusion
 
After all the long theory in the previous two articles, this is our first practical implementation. If you are very new to WCF applications then I hope you have understood the concept and implementation. In a future article, we will explore WCF more. Happy day.