WCF And WCF Service Components

WCF Introduction

WCF is a unified communication framework that integrates all the Communication frameworks like ASMC/WSE, .NET REMOTING, DCOM/COM and MSMQ into one logical model.

WCF increases the productivity in the sense that you learn only one programming model rather than learning multiple models as in the other communication frameworks describe above. Each one of them has its own programming model so if developers want to learn or work in any of them, they need to understand each framework programming model separately.

In WCF, you write code in one way and have many ways of communication, like in one case if you want SOAP based communication through MSMQ or in another case Restful based communication through Http etc.

WCF Service Components

Each WCF service has three main components

  1. Address
  2. Binding
  3. Contract

Address

Address is called end-point also. They represent service location or contain a piece of information about where to send a message. Address specifies protocol (Http) and host (www.example.com) for using service.

Bindings

Binding specifies how to send a message and which protocol is used i.e. SOAP, or how a client can communicate with the service. Bindings also specify security constraints and other options. Some of the built-in bindings that WCF provides and their usage scenarios are as follow.

Binding Types Usage Scenario
 WebHttpBinding For Restful Communication using Http
 BasicHttpBinding For SOAP Communication using Http
 NetTcpBinding For .Net-To-.Net(Cross Machine) Communication using TCP

Based on requirements, you can create custom bindings too.

Example 

  1. <configuration>  
  2.     <system.serviceModel>  
  3.         <services>  
  4.             <service name=”servicename”>  
  5.                 <endpoint address=”http://server/servicename” binding=”webHttpBinding” contract=”IExampleService” /> ……… </service>  
  6.         </services>  
  7.     </system.serviceModel>  
  8. </configuration>   

Contract

Contract is an interface where you define the methods that you want to expose. Clients interact with services through contract.

Example

  1. [ServiceContract]  
  2. public interface InterfaceName {  
  3.     [OperationContract]  
  4.     string Example(string name)  
  5. }