Getting started With WCF Service For Beginners

In this article, I will demonstrate you step by step about WCF. It is a brief introduction about WCF.

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.

wcf

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.

  1. <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.

  1. <endpoint address="http://localhost:8731/DemoService/" binding="basicHttpBinding"   
  2. contract=" DemoService.IDemo">  
Service

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:
  1. Service contracts
  2. Data contracts
  3. Message contracts
  4. Fault Contract
  5. 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.

  1. [ServiceContract]  
  2. public interface IMyService  
  3. {  
  4.   
  5.    // All the Operation goes here  
  6. }  
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.
  1. public interface IMyService  
  2. {  
  3.    [OperationContract]  
  4.    string GetData(int myValue);  
  5.   
  6.    [OperationContract]  
  7.    int AddData(int first, int second);  
  8. }  
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.
  1. [DataContract]  
  2.  public class MyClass  
  3.  {  
  4.      private string _Name;  
  5.   
  6.      private string _City;  
  7.   
  8.   
  9.      [DataMember]  
  10.      public string Name  
  11.      {  
  12.          get { return _Name; }  
  13.          set { _Name = value; }  
  14.      }  
  15.  }  
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.
  1. [ServiceContract]  
  2. public interface IMyService  
  3. {  
  4.     [OperationContract]  
  5.     [FaultContract(typeof(Employee))]  
  6.     Employee GetDetails(string Name);  
  7. }  
  8.   
  9. [DataContract]  
  10. public class Employee  
  11. {  
  12.     private string _Name;  
  13.   
  14.     private string _City;  
  15.   
  16.     [DataMember]  
  17.     public string Name  
  18.     {  
  19.         get { return _Name; }  
  20.         set { _Name = value; }  
  21.     }  
  22.   
  23.     [DataMember]  
  24.     public string City  
  25.     {  
  26.         get { return _City; }  
  27.         set { _City = value; }  
  28.     }  
  29. }  
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.
  1. [MessageContract]  
  2. public class Employee  
  3. {  
  4.     [MessageHeader]  
  5.     public Operation EmpName;  
  6.   
  7.     [MessageHeader]  
  8.     public string city;  
  9.       
  10.     [MessageBodyMember]  
  11.     private Home Address;  
  12.               
  13.     [MessageBodyMember]  
  14.     public int Salary;  
  15. }  
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.

Hosting process

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.