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:
- Understand WCF:Part-1:What is SOA, Services and Messages
- Understand WCF: part -2: Understand RESTful Service
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- namespace NameService
- {
- [ServiceContract]
- public interface ITemperatureService
- {
- [OperationContract]
- string GetTemperature(int Faren);
- }
- }
Now, we need to implement this interface in an actual class. Here is the class structure.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- namespace NameService
- {
- public class TemperatureService : ITemperatureService
- {
- public string GetTemperature(int Faren)
- {
- return Convert.ToString(((Faren - 32) / 9) * 5);
- }
- }
- }

Join the conversation! Your thoughts help the community grow.