Print Friendly, PDF & Email

In this post, I am going to explain in detail about Windows Communication Foundation architecture, bindings or behavior, and types of contracts. We all know that WCF is 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

WCF follows the Service Oriented Architecture (SOA).

Types of contract in WCF

In WCF all services exist as contracts. Contracts are a 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 the service contract attribute on top of a class where the first client request comes. It also defines the operation that service can provide as well as what the service will create by using the 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 a Service contract.

Data Contract

A data contract is used to describe a custom data type that is exposed to the client. It defines data types, which were passed to and from service.

The Data types like string and int are identified by the client as they are already mentioned in the Schema of XML definition language document. However, the custom-created class or data types cannot be identified by the client, e.g. the employee data type.

Like this, by using the Data Contract, we could make the client aware of the data type of employee who is 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 the 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 this, we have a Fault Contract. Fault Contracts provide a documented view for an error that has occurred in the service. Also, it helps us to easily identify what error has occurred.

More 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 used remotely or acted upon and can easily be updated independently, like retrieving credit card statements online.

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

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

Binding and Behavior of WCF

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

How can it be done? 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.

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

Example

In the information for the below configuration, we have mentioned the behavior at the Service level. Although, the service behavior that is being mentioned by us is the service metadata node that will only be attributing httGetEnabled=’true’. This attribute will also be used 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. In general, they will be loading the service configuration endpoints, applying the settings, and 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 the 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 hosted by the following mechanism

  • IIS: Basically, Internet Information Service provides a number of advantages if a Service uses HTTP as protocol. It never requires a Host code to activate the service, and it will be automatically an activated service code.
  • Windows Activation Service (WAS): WAS is a new process for the activation mechanism that is used to ship with IIS 7.0. to go with HTTP based communication. WCF could have also used WAS so as to provide the message-based 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 hosted as a Windows Service so that it is under the 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 Windows Communication Foundation its contract types bindings or behavior and architecture.

My next post will be based on WCF’s advanced features and security related to this service. 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