Pages

Tuesday, June 30, 2009

All about WCF (Introduction)


Here in my first series of article I will be sharing top to bottom of the WCF (The Indigo).

WCF is the great way to approach large multi-system message based communication. It seamlessly support Transaction Management, Security Implementation, Exception and Fault handling. Concurrency management etc.

Usage of WCF comes with the great amount of flexibilities' to handle. WCF is a framework for services. The user can Create, Configure, Host and consume the services. All the services are mutually agreed on SOAP concept and communicating between WCF based clients.

WCF has mainly two categories of classes:

1. Service Model Class (Handles Service Configurations & Validations).

2. Channel Classes (Handles mode of communication and related functionalities).

Each WCF service can be associated with one ore more service endpoints. One service can be hosted in WCF with different mode of communication channel. Depending upon the usage and target client WCF Service can be configured (I will focus this more on this in my up coming articles).

Configuring endpoints require (A, B & C):

1. Address ('A') = Where?

2. Binder ('B') = How?

3. Contract ('C') = What?

Address: The address means the URL/URI where the service is located. Where is the service located?
Binder:
The mode ofcommunication. How to communicate with the service?
Contract:
The Service. What is my service?

With the XML Based configuration approach OR with the help of coding we can configure the endpoints.

Hosting WCF Service

WCF Service is can be segregated in to two parts normally:

1. Service Contract

2. Service Implementation

Service Contract:
Service Contract is just the collection of functionality that Service will be exposed. In a way you can assume that a Service Contract is just like the interfaces in .NET.

For example

In above example, I have created a sample service with just one operation i.e. MyOperation with one parameter. As you can see i have used Attributes. ServiceContract attributes comes under System.ServiceModel. Including this DLL as in reference list is must for WCF Service Host and WCF Consumer.

You can create multiple operations by using "OperationContract" attribute. By specifying OperationContract attribute means the operation will be available for WCF Service and the WCF client can consume that.

Service Implementation:
As named Service implementation is the implementation bin of WCF Service Contract. You can implement this WCF Service Interface in 'N' number of classes.

For example

In above example I have created "HelloIndigoService" which is implementing our "IHelloIndigoService" service interface.

WCF Service can be self-hosted service but to start with we will see simple .NET consol based hosting mechanism.

Attributes of Service Contract

Using declarative model you can apply different parameters (as per your requirements) on WCF Service Contract:

CallbackContract
User can specify the call-back service that represents opposite service contract in duplex message exchange.

ConfigurationName
User can specify the pre-created configuration name which is/are defined in Application's configuration file.

Example

Name:

Name property is impacting on WSDL generation. With the help of this property user can specify XML Root Tag name of WSDL. Unlike the older messaging mechanisms, WCF provides great deal of control over WSDL generation. I will discuss more about forming the WSDL with OperationContract later in this article.

Example

Namespace:

Namespace is also related with WSDL formation. After you generated the proxy of WCF Service; the given namespace will be appear in WSDL Namespace.

Example

ProtectionLevel:

User needs to use ProtectionLevel according to their service requirements and the operations that service will be exposing. There are three different ProtectinLevels are available.

1. None:

2. Sign: The protected part is signed digitally to ensure the temper less submission of data

3. EncryptAndSign: This will encrypt data before sighed it digitally.

More details on ProtectionLevel : http://msdn.microsoft.com/en-us/library/aa347692.aspx

SessionMode:
Specifying the attribute with "Allowed", "NotAllowed" and "Required". As name says "NotAllowed" will not support Session. The main difference between "Allowed" and "Required" is: "Allowed" will support Session persistent if incoming binding supports. Where as "Required" will throw an error in case binding is not supporting.