Searching...
Friday, 19 March 2010

WCF Architecture Explained

Windows Communication Foundation (WCF) simplifies development of connected applications through a new service-oriented programming model. It is a runtime and a set of APIs for creating systems that send messages between services and clients.

In the world there are lots of distributed communication technologies exist. Some of them are:

ASP.NET Web Services (ASMX)
Messaging (MSMQ)
COM+
.NET Remoting

Each of these technologies has their own functionality. In cases where developer needs to use all these functionalities combined it becomes difficult to manage multiple technologies in an application.

To address this issue Microsoft developed WCF. It is unification of .Net framework communication technologies such as .Net Remoting, Web services, MSMQ and COM+.

In a distributed communication unit of functionality is exposed as service. Client consumes this service through the binding channel provided by the service.



Windows Communication Foundation (WCF) is an SDK for developing and deploying services on Windows. It provides essential for developing services, managing runtime facilities, establish binding and hosting service.

In .Net framework WCF functionality is included in a single assembly called System.ServiceModel.dll in the System.ServiceModel namespace. WCF is divided in to four components

Contracts
Service runtime
Messaging
Activation and hosting
Contracts

In WCF, all services are exposed as contracts. Contract is a neutral way of describing the service what it does. Mainly there are four types of contracts -

Service Contract

This contract describes all the available operations that client can perform on the service.
ServiceContract attribute is used to define the service contract. We can apply this attribute on class or interface. ServiceContract attribute exposes a CLR interface (or a class) as a WCF contract.

OperationContract attribute, is used to indicate explicitly which method is used to expose as part of WCF contract. We can apply OperationContract attribute only on methods, not on properties or indexers.

[ServiceContract] applies at the class or interface level.
[OperatiContract] applies at the method level.

Data Contract

This contract defines the data types that passed in and out to the service.
[DataContract] attribute is used at the custom data type definition level, i.e. at class or structure level.
[DataMember] attribute is used to fields, properties, and events.

Fault Contract

This contract describes about the error raised by the services.
[FaultContract(<<type of Exception/Fault>>)] attribute is used for defining the fault contracts.

Message Contracts

This contract provides the direct control over the SOAP message structure. This is useful in interoperability cases and when there is an existing message format you have to comply with.
[MessageContract] attribute is used to define a type as a Message type.
[MessageHeader] attribute is used to those members of the type we want to make into SOAP headers
[MessageBodyMember] attribute is used to those members we want to make into parts of the SOAP body of the message.

Policies and Binding – Specify conditions required to communicate with a service e.g. security requirement to communicate with service, protocol and encoding used for binding.

Service runtime

It contains the behaviors that occur during runtime of service.

Throttling Behavior – Controls how many messages are processed.
Error Behavior – Specifies what occurs, when internal error occurs on the service.
Metadata Behavior – Tells how and whether metadata is available to outside world.
Instance Behavior – Specifies how many instance of the service has to be created while running.
Transaction Behavior – Enables the rollback of transacted operations if a failure occurs.
Dispatch Behavior – Controls how a message is processed by the WCF Infrastructure.
Message Inspection – Facility to inspect parts of a message.
Parameter Filtering – Enables preset actions to occur based on filters acting on message headers.
Concurrency Behavior – Controls whether an instance of a service processes messages sequentially or concurrently.

Messaging

Messaging layer is composed of channels. A channel is a component that processes a message in some way, for example, by authenticating a message. A set of channels is also known as a channel stack. Channels are the core abstraction for sending message to and receiving message from an Endpoint. Broadly we can categories channels as

Transport Channels – Handles sending and receiving message from network. Protocols like HTTP, TCP, name pipes and MSMQ.
Protocol Channels – Implements SOAP based protocol by processing and possibly modifying message E.g. WS-Security and WS-Reliability.

Activation and Hosting

Services can be hosted or executed, so that it will be available to everyone accessing from the client. WCF service can be hosted by following mechanism

IIS – Internet information Service provides number of advantages if a Service uses HTTP as protocol. It does not require Host code to activate the service, it automatically activates service code.
Windows Activation Service (WAS) – It is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes.
Self-Hosting (.EXE) – WCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.
Windows Service – WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM).

0 comments:

Post a Comment