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.
- 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);
- }
- }
Implement the interface
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);
- }
- }
- }
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.
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.
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.
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.
Now we will call the service class to consume the service. Have a look at the following code.
- using System;
- using System.Text;
- using Client.TemperatureService;
- namespace Client
- {
- class Program
- {
- static void Main(string[] args)
- {
- TemperatureService.TemperatureServiceClient obj = new TemperatureServiceClient();
- Console.WriteLine("Value is :- " + obj.GetTemperature(100));
- Console.ReadLine();
- }
- }
- }
Don't forget to include the namespace of the WCF service application.
Here is sample 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.