In this blog, I am going to explain in detail about Windows Communication Foundation architecture, bindings or behavior and types of contract. We all know that WCF is a very useful functionality for every mobile and web application.

WCF is used to consume by different end users to make a lightweight and secure application.

WCF Architecture

The below image describes the major components of WCF. WCF follows the Service Oriented Architecture (SOA).

Types of contract in WCF

In WCF all services are expose as contracts. Contracts are neutral platform and a  specified way of describing what a service does. Specifically, we have four types of contracts that are available in WCF.

Service Contract

We define service contract attribute on top of a class where first client request comes. It also defines the operation that service can provide as well as the service will  create by using Service and Operational Contract attribute.

For Example, we have a service to know the temperature of the city which will be based on zip code, this service will be called as Service contract.

Data Contract

A data contract is used to describe custom data type which is exposed to the client like this it defines data types, which were pass to and from service.

The Data types like string as well as int are identify by the client as it is already mentioned in the Schema of XML definition language document, but the custom created class or data types cannot be identified by client e.g. employee data type.

Like this, by using the Data Contract we could make the client aware of the data type of employee which are either returning or passing some parameters to the method.

Message Contract

By default, the format for SOAP message is provided by the WCF runtime for communication between Client and the service. If that is not meeting our requirements then we can create our own message format.

Fault Contract

We assume that the service is not working in the client application. We want to know the real cause of the problem. How will we be able to know the error? To resolve we have Fault Contract. Fault Contract provides documented view for an error that has occurred in the service as well as this will helps us to easily identify, what error has occurred.

About Service Oriented Architecture (SOA)

WCF  is a style of the software design pattern where the services are provided to the other components by other application components, although the communication protocols over a network. The WCF service will be a discrete unit of functionality that can be use remotely or act upon and can easily be update independently, like retrieving credit card statement online.

The SOA will have its own basic principles that are independent of products, vendors and technologies. A service-oriented architecture has four properties see below points.

  • This service logically represents a business activity with a specified outcome.
  • This is self-containg.
  • It is a black box for its consumers.
  • It may consist of other underlying services.

Binding and Behavior of WCF

Binding

The simple definition of Binding describes how the end user or client will communicate with service. We can understand how end user communicates with binding using below scenario with an example. Let’s assume a scenario say, we are creating a service that has to  use by two type of client. One client will access SOAP by using HTTP and another client will be accessing binary using TCP.

How can it be done? Along With the Web service, it is really difficult to achieve, but with WCF  we only need to add some extra endpoints that are present in the configuration file.

For Example:-

<system.serviceModel> 
 <services>
 <service name="MathService"  behaviorConfiguration="MathServiceBehavior"> 
 <endpoint address="http://localhost:8090/MyService/MathService.svc"   contract="IMathService"          binding="wsHttpBinding"/>
 <endpoint address="net.tcp://localhost:8080/MyService/MathService.svc" contract="IMathService"          binding="netTcpBinding"/> 
 </service> 
 </services>
 <behaviors>
 <serviceBehaviors>
 <behavior name="MathServiceBehavior">
  <serviceMetadata httpGetEnabled="True"/>
  <serviceDebug includeExceptionDetailInFaults="true" />
  </behavior>
 </serviceBehaviors>
 </behaviors> 
 </system.serviceModel>.

See how simple it is in WCF.  Microsoft is trying to make things pretty simple according to its scope and common behaviors affect all the endpoints globally, service behaviors will be affecting only the service related aspects, endpoint behaviors will also be affecting only endpoint-related properties, and operation-level behaviors will affect particular operations.

For Example

In the information for the below configuration, We have mentioned the behavior at the  Service level. Although, the service behavior that is being mention by us is the service metadata node that will only be attributing httGetEnabled=’true’. This attribute will also be use as to specify the publication of the service metadata. Similarly, we could have easily added more behavior to the service. For example:-

<system.serviceModel>
 <services>
<service name="MathService"  behaviorConfiguration="MathServiceBehavior">
 <endpoint address="" contract="IMathService" binding="wsHttpBinding"/> 
 </service>
  </services> 
    <behaviors>
        <serviceBehaviors>
         <behavior name="MathServiceBehavior"> 
          <serviceMetadata httpGetEnabled="True"/> 
          <serviceDebug includeExceptionDetailInFaults="true" />
   </behavior>
 </serviceBehaviors>
 </behaviors> 
  </system.serviceModel>.

Types of service host in WCF

Service Host objects are the ones in the process of hosting the WCF service & are used for registering the endpoints and in general They will be loading the service configuration endpoints, it will be applying the settings and it will also start the listeners to be capable of handling the incoming request and System.ServiceModel.ServiceHost namespace holds this object.

These Services are very much capable of being host or execute easily so that it can easily be available to everyone while trying to access from the client. WCF services can be easily host by the following mechanism

  • IIS:- Basically, Internet Information Service provides a number of advantages if a Service uses Http as protocol. It never requires Host code to activate by the service, it will be automatically an activate service code.
  • Windows Activation Service (WAS):- WAS is a new process for the activation mechanism that is use to ship with IIS 7.0.to go with HTTP based communication, WCF could have also use WAS so as to provide the message base activation over the other protocols, like  TCP and named pipes.
  • Self-Hosting:- WCF services can also be self-host as a console application, Win-Forms or WPF applications having graphical UI.
  • Windows Service:- WCF can also be hosting as a Windows Service so that it is under control of the Service Control Manager (SCM).
Hosting Environment Supported protocol
Windows console and form application HTTP,net.tcp,net.pipe,net.msmq
Windows service application (which was formerly known as NT services) HTTP,net.tcp,net.pipe,net.msmq
Web server IIS6 http, wshttp
Web server IIS7 – Windows Process Activation Service (WAS) HTTP,net.tcp,net.pipe,net.msmq

A summary of hosting options and supported features

Feature Self Hosting IIS Hosting WAS Hosting
Executable Process/ App Domain Yes Yes Yes
Configuration App.config Web.config Web.config
Activation Manual at startup Message-based Message-based
Idle-Time Management No Yes Yes
Health Monitoring No Yes Yes
Process Recycling No Yes Yes
Management Tools No Yes Yes

Choosing an Appropriate WCF binding

Depending upon our requirements, we can choose a binding for our service as shown below in the diagram:

WCF bindings comparison

For example, see the below chart

Binding

Protocol/Transport

Message Encoding

Security

Default Session

Transaction

BasicHttpBinding Http, Https Text None No
WSHttpBinding Http, Https Text Message Optional Yes
WSDualHttpBinding Http, Https Text Message Yes Yes
NetTcpBinding TCP Binary Transport Optional Yes
NetNamedPipeBinding Named Pipe Binary Transport Yes Yes
NetMsmqBinding MSMQ Binary Transport Yes Yes
WSFederationHttpBinding Http, Https Text Message Yes Yes
NetPeerTcpBinding P2P Binary Transport
MsmqIntegrationBinding MSMQ Not Supported Transport Yes Yes

CONCLUSION

I hope this article will help you understand about Windows Communication Foundation its contract types bindings or behavior and architecture.

My next blog will be based on WCF advance features and security related with this servuice. For further queries, feel free to raise them in the comments section below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here