Getting Started With WCF

Introduction

Windows Communication Foundation (WCF) is the framework for building, configuring and deploying network-distributed services. WCF is the latest service-oriented technology. WCF is a programming platform and runtime system for creating the services. WCF is the unified programming model included in the .NET Framework 3.0. WCF is a collection of various features, like Web Services, Remoting, COM+ and MSMQ. The main feature of WCF is to provide a common platform for all .NET communication. WCF provides a wide range of features, including security and communication protocol support.


Figure 1: WCF Basic diagram 

Advantages of WCF
  • WCF provides better security and reliability compared to ASMX web Services.
  • In WCF, we don't need to change as much in the code for implementing the security model and changing the binding. We just need to make small changes in the configuration to fulfill the requirements.
  • WCF is interoperable with the other services compared to .NET remoting where the client and service must be .NET.

Binding in WCF

Binding defines an important role. Binding specifies how a client will communicate with the services and the address where the endpoint is hosted.

Types of bindings in WCF

  • BasicHttpBinding

    It is the Basic Web service for the communication. No security by default.

  • NetMsmqBinding

    It provides the communication among WCF applications using queuing.

  • NetTcpBinding

    It provides the communication among WCF applications across computers. It also supports duplex contracts.

  • NetPeerTcpBinding

    It provides communication among computers across peer-to-peer services. It supports duplex contracts.

  • WSDualHttpBinding

    Web Services with a duplex contract and transaction support.

  • NetNamedPipeBinding

    It provides the communication among WCF applications on the same computer. Its supports duplex contracts and transactions.

Contracts in WCF

The main use of contracts is to allow the client and services agree as to the types of operations and structures they will use during the communication process. It also shows the formal agreements between client and service to define a platform-neutral and standard for describing that what the service does.

WCF has the following four contracts:

  • Service Contract
  • Message Contract
  • Data Contract
  • Fault Contract

Service Contract

The main purpose of a Service Contract is to define the interface for the services and the service contract also describes the operation that the service can provide. The service contract is created using the Service and Operational Contract attributes. To create a service contract we define the interface with related methods and decorate the interface with the ServiceContract Attribute to indicate it is a service contract.

Syntax

  1. [ServiceContract]  
  2. public interface ISevice2  
  3. {  
  4. //TODO: Here perform your service operations  
  5. }

Message Contract

The Message Contract is provided by the WCF runtime for the communication between Client and Services. In the Message Contract we can create our own message format so it can be possible using the Message Contract attribute.

Syntax

  1. [MessageContract]  
  2. public class Person   
  3. {  
  4.     [MessageHeader]  
  5.     public Operation Name;  
  6.       
  7.     [MessageHeader]  
  8.     public string Address;  
  9.       
  10.     [MessageBodyMember]  
  11.     private Home_Phone;  
  12.       
  13.     [MessageBodyMember]  
  14.     private PersonalID;  
  15.       
  16.     [MessageBodyMember]  
  17.     public int age;  
  18.       
  19. }

Data Contract

The Data Contract provides the description of the custom data type exposed to the client. So it defines the data types that are passed to and from the service. A Data Contract describes the external format of data passed to and from the service operation. The Data Contract also defines the structure and types of data exchanged in the service message.

  1. [DataContract]  
  2. public class Student   
  3. {  
  4.     private string _Name;  
  5.     private string Address;  
  6.     [DataMember]  
  7.     public string Name   
  8.     {  
  9.         get   
  10.         {  
  11.             return Name;  
  12.         }  
  13.         set   
  14.         {  
  15.             _Name = value;  
  16.         }  
  17.     }  
  18. }

Fault Contract

A Fault Contract provides a documented view for errors occurring in the service to the client. A Fault Contract handles the error or exception and understands the cause of an error occuring in the WCF service. By default, when we throw an error from a service, it will not be received by the client side, so to reduce the problem, WCF provides a convey message to the client from the service using the Fault Contract.

  1. [ServiceContract]  
  2. public interface IGetDetailsService  
  3. {  
  4.     [OperationContract]  
  5.     [FaultContract(typeof(Student))]  
  6.     Student GetDetails(string Name);  
  7. }  
  8.   
  9. [DataContract]  
  10. public class Student   
  11. {  
  12.     private string _Name;  
  13.   
  14.     private string _Address;  
  15.   
  16.     [DataMember]  
  17.     public string Name   
  18.     {  
  19.         get   
  20.         {  
  21.             return _Name;  
  22.         }  
  23.         set   
  24.         {  
  25.             _Name = value;  
  26.         }  
  27.     }  
  28.   
  29.     [DataMember]  
  30.     public string Address   
  31.     {  
  32.         get   
  33.         {  
  34.             return_Address;  
  35.         }  
  36.         set   
  37.         {  
  38.             _Address = value;  
  39.         }  
  40.     }  
  41.   
  42. }

Summary

This article explained the basics of WCF, contracts of WCF Services and the binding in the WCF Services.

I hope this article will be helpful for the beginners, if they want to start working in the WCF.