What is WCF Service?
WCF stands for Windows Communication Foundation and is part of .NET 3.0. WCF is a Microsoft platform for building distributed and interoperable applications.
WCF supports multiple languages and multiple platforms.
Why Should We Use WCF Service?
Let’s take these two scenarios:
We have two clients and we need to implement a service for them:
- The first client is using Java Application to interact with our service. So for interoperability, this client wants messages in XML format and the protocol to be HTTP.
- The second client uses .NET, so for better performance, this client wants messages in binary format and the protocol to be TCP.
Without WCF Service
- To satisfy the first client requirement, we use web service.
- To satisfy the second client requirement, we use .NET Remoting.
These are two different technologies, and have completely different programming models. So the developers have to learn different technologies.
So to unify and bring all technologies under one roof, Microsoft has come with a new programming model called WCF-Windows Communication Foundation.
You implement one service and we can configure as many end points as we want to support all the client needs. To support the above 2 client requirements, we would configure 2 end points. In the endpoint configuration, we can specify the protocols and message formats that we want to use.
So, the conclusion is that:
- A web service to exchange messages in XML format using HTTP protocol for interoperability
- A remoting service to exchange messages in binary format using TCP protocol for performance
Along the way, we will get a feel of how different these technologies are.
And last, we will discuss implementing a single WCF Service and configuring different ‘end points to support different transport protocols and message formats.
Now we create one WCF Calculator Service:
- Open your Visual Studio. File->new website->select Visual C#->select WCF service.
- This is default Iservice.cs.
In which there are two attributes:
Iservice interface and
OperationContract: For methods, here it declares all methods which are accessible at client side
Each method has its own
Now, we create two methods for our calculator service.
- This is default Service.cs
In which it implements Iservice interface and in this class, all the declared methods of Iservices.cs are defined.
- This is shown in the figure below:
- In Service.cs, right-click on
IService and click on Implement Interface and select Implement Interface as in screen. Then, you can see whatever you have declared in Iservice is now implemented here and write your logic.
- Save your files and view in browser Services.cs.
- Your WCF service is running on the browser. Now copy that URL and later on at client side, it will be pasted.
- Now we create Client side app which uses WCF.
- Take two textboxes, one label and one button from the toolbox and drop in from1 as in screen.
- Right click on project and click on Add Service Reference.
- Paste that URL which is copied earlier, then click on Go. You can see your service with
IService and your method comes, then click on Ok.
- Fire button click event of button which is in from1.Create object of WCF service and base on this object call method. Run the application and pass value. You can see the total is reflected at
- When you run this application, you will get an output like this: