WCF or Windows Communication Foundation is a platform which is used to create distributed application service. It is a cross platform application that means WCF service can be used by client which is created in .NET technology or java technology or any others. WCF is also known as Indigo (this was its earlier name), but when it introduced with .NET Framework, it is known as WCF.
WCF is introduced with .NET Framework 3.0 by Microsoft in 2006.
See the above image; you can see that WCF is a common technology that provides all the features of the above technologies.
What is the use of WCF?
WCF is a programming model to create service which can be accessible to a wide range of client. It supports variety of protocol, encoding, security, etc. It is more secure compared to Web Services.
Example:
Let take an example, if you create a service for your client which use HTTP protocol to communicate the service. So, let's assume you created your service using Web Service. It is supposed that in future the other client ask for the same service, but he/she is using the TCP Protocol, So, what will you do here.
Here you need to do one more effort to create a new service for new client as per the requirement. So, you did double effort for the same service.
Here WCF comes and resolve this problem. If you choose WCF service, then you can expose your service to number of clients without creating any new service or new development.
You just need to make some configuration changes to expose the service to a particular client.
ABC in WCF
ABC stands for Address, Binding and Contract. If you expose your service to a new client, then you just need to add ABC in configuration.
- <endpoint address="ServiceUrlGoesHere" binding="TypeOfBinding" contract="NameOfContract" />
A stands for Address: Address means, the address of service. I mean to say where you hosted your service which is going to expose. You just need to define your service address inside the configuration.
B stands for Binding: Binding means how your service will communicate the outer world and which protocol will be used by the client to access the service?
C stands for Contract: Contract means what task is done by service. We need to define contract name inside the endpoint which describe that which are the operations done by the service.
- <endpoint address="http://localhost:8731/DemoService/" binding="basicHttpBinding"
- contract=" DemoService.IDemo">
Contracts in WCF
Contracts are very useful to make a WCF service application. It defines what type of protocol does service use, what type of operation will be done by the service, what type of data will be sent over the protocol.
Here are theg Contracts which are available in WCF:
- Service contracts
- Data contracts
- Message contracts
- Fault Contract
- Operation contract
Service contracts
Service contract describes about all the operations which we will perform by the service. It can be an Interface or a class, I suggest always use an Interface rather than a class because of security purpose. [ServiceContract] attribute is used to define the service contract.
- [ServiceContract]
- public interface IMyService
- {
-
-
- }
Operation contract
Operation contract defines all the action or we can say method/function which will execute by service when client will consume it. These methods are used to exchange the data between client and service. You need to use [OperationContract] attribute to define the operation contract in WCF.
- public interface IMyService
- {
- [OperationContract]
- string GetData(int myValue);
-
- [OperationContract]
- int AddData(int first, int second);
- }
Data Contract
Data contract is used to define your data over the network. Here you define the get set properties. But the main use of data contract is that it is responsible for serialize and de-serialize the complex data over the network. Data contract defines how data types are serialized and de-serialized.
- [DataContract]
- public class MyClass
- {
- private string _Name;
-
- private string _City;
-
-
- [DataMember]
- public string Name
- {
- get { return _Name; }
- set { _Name = value; }
- }
- }
Fault Contract Sometimes, it happens with the service that some exception is occurred at runtime and user gets some unwanted data which are not required to show to client. So, to handle the exception in WCF, we use the Fault contract.
Here we can’t use try-catch block to handle these type of error because these are used with technology specific error. But as you know that our wcf service can be consumed by any of the client which may or may not be using the same technology client application to consume the service.
- [ServiceContract]
- public interface IMyService
- {
- [OperationContract]
- [FaultContract(typeof(Employee))]
- Employee GetDetails(string Name);
- }
-
- [DataContract]
- public class Employee
- {
- private string _Name;
-
- private string _City;
-
- [DataMember]
- public string Name
- {
- get { return _Name; }
- set { _Name = value; }
- }
-
- [DataMember]
- public string City
- {
- get { return _City; }
- set { _City = value; }
- }
- }
Message contracts
By default, WCF supports SOAP based message format. But if customer requirement is something different because he/she do not want to use SOAP based message format, then you need to define your own message format. So, you can do this by using the Message contracts. For using Message Contract you just need to add [
MessageContract] attribute.
- [MessageContract]
- public class Employee
- {
- [MessageHeader]
- public Operation EmpName;
-
- [MessageHeader]
- public string city;
-
- [MessageBodyMember]
- private Home Address;
-
- [MessageBodyMember]
- public int Salary;
- }
Hosting of WCF Service
If you create a WCF service and want to expose to your client, then here you need to host your service. You can host your wcf service by different ways. Services can be hosted on IIS or it can be self-hosted, etc.
The following are the ways to host a WCF service to expose to client.
IIS Hosting
You can host your service on IIS [Internet information Service]. It provides number of advantages if Service hosted on IIS and uses Http as protocol. It auto activates the service.
Windows Activation Service Hosting Window Activation Service (WAS) hosting is a way to host your service that ships with IIS 7.0 for HTTP based communication; WCF service can also use WAS to provide message-based activation over other protocols like TCP and named pipes.
Self-Hosting If you want the service to not host at any other place you can make it self-hosted. Service can be self-hosted in Console Application and Window Forms application.
Windows Service Hosting You can also host your service as window service which can start and stop based on your requirement. It uses all the features of window service.
Advantages of WCF - It supports for different protocol to communicate with client.
- It provides more secure message format over the network.
- It also supports Load balancing & support scaling.
- You can host a WCF service on IIS, WAS, Self-hosting, Windows services.
- It supports Multiple Message Patterns.
Hope you all enjoyed this article.