Introduction to WCF

WCF stands for windows communication foundation which is used to communicate with cross Platform application and to develop network distributed applications.
Previously, WCF was known for the Indigo service and later on with .Net Framework is known as WCF, WCF was first introduced with .Net Framework 3.0 in 2006 later on its become the most popular programming language for network distributed application because it provides all combined features of Web service, Remoting, MSMQ and com+ on a single platform without using different services for different requirements.
Let us understand with the following diagram.


From the above diagram, I am trying to give you a message that WCF is the common technology to provide the feature of all technologies being used to communicate for different purposes.

Why WCF ?

WCF Provides a wide range of features including communication protocol support, security etc. Let us consider the following example.



This is a normal communication of a Web service and a client in which the client's requirement is to exchange the data using HTTP Protocol.

In the above example, the Web service communicates with the client using HTPP protocol and only HTTP protocol supports the web service. Suppose in the future clients want to exchange data using TCP as well as HTTP without creating a separate service. Then with the Web service it's not possible, we can achieve it by using WCF service.




From the above diagram I am trying to explain that WCF Service can exchange data or message in any format that is SOAP, Binary using a wide range of communication protocol such as HTTP,TCP etc.

WCF Architecture 

The WCF architecture consists of the following components.
  • Contracts.
  • Service Run time.
  • Messaging.
  • Activation and Hosting .
Let us briefly learn about the preceding terms.

Contracts in WCF

Contracts are useful to build the WCF service application. The Contracts defines what protocol (binding) the service uses, how communication will be done, what message exchange format to use and much more.
The following are the contracts which are available in WCF.
  • Service contracts
  • Data contracts
  • Message contracts
  • Fault Contract 
  • Operation contract
  • Policies and Binding
Service contracts

The service contracts nothing, but it defines the interface for the service. It can be defined as follows.

Syntax

   [ServiceContract]
    public interface IService1
    {

        // TODO: Add your service operations here
    }

Operation contract

The operation contract defines the method which is exposed to the client to exchange the information between the client and the server. The operation contract describes what functionality to be given to the client such as Addition, subtraction etc.
It can be defined as

   public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
  
    }

Data Contract


Data Contracts are the same as get and set properties, but the difference is that data contract in WCF is used to serialize and deserialize the complex data. It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through de-serialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
It can be defined as follows.

    [DataContract]
    public class Student
    {
        private string _Name;

        private string _City;


        [DataMember]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
}

Fault Contract


Fault contract is used to handle the exception and know the cause of the error which occurs in WCF service. When we develop a managed application or service, we will handle the exception using try-catch block. But these exceptions handling are technology specific.
In order to support interoperability and client will also be interested only in what went wrong? Not on how and where the error was made.
The following is the syntax to raise the custom error in WCF.

[ServiceContract]
    public interface IGetDetailsService
    {
        [OperationContract]
        [FaultContract(typeof(Student))]
        Student GetDetails(string Name);
    }

    [DataContract]
    public class Student
    {
        private string _Name;

        private string _City;

        [DataMember]
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        [DataMember]
        public string City
        {
            get { return _City; }
            set { _City = value; }
         }
}

Message contracts

Default SOAP message format is provided by the WCF run time for communication between the client and service. If it is not meeting your requirements, then we can create our own message format. This can be achieved by using Message Contract attribute.
It can be defined as

[MessageContract]
public class Person
{
  [MessageHeader] public Operation Name;
  [MessageHeader] public string city;
  [MessageBodyMember] private Home Address;
  [MessageBodyMember] private Home Streat;
  [MessageBodyMember] public int age;
}

Policies and Binding in WCF


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

Service Run-time in WCF

It contains the behaviors that occur during the run-time of service. The following behaviors are managed by the service run-time layer.
  • Throttling Behavior
  • Error Behavior 
  • Metadata Behavior
  • Instance Behavior 
  • Transaction Behavior
  • Dispatch Behavior 
  • Concurrency Behavior
  • Parameter filtering
  • Message inspection
Throttling Behavior: It 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 the outside world.
Instance Behavior: Specifies how many instances of the service have 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.
Concurrency Behavior:  It's used to control how many threads can access a given instance of service.
Parameter filtering: It filters the message headers and executes preset actions based on the filters of the message headers. 
Message inspection: It's used to inspect a specific part or all parts of the message through service.

Messaging in WCF Service

The 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. Generally, we can categories channels as follows:
  • Transport Channels
  • Protocol Channels  
Transport Channels 

Handles sending and receiving messages from the 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 in WCF

Services can be hosted or executed, so that it will be available to everyone accessing the client. The WCF service can be hosted by the 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 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


The WCF service can be self-hosted as a console application, Win Forms or WPF application with a 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).

Advantages of WCF
  • Support for different protocols
  • Security over transport as well as Message level
  • Easy to implement MSMQ and REST Service.
  • Load balancing & support scaling.
  • It can control concurrency.
  • It can be hosted on IIS, WAS, Self hosting, Windows services.
  • Multiple Message Patterns.
  • Unhanded Exceptions does not return to the client as SOAP faults. WCF supports better exception handling by using Fault Contract.
Note

Please refer my following articles for complete understanding

Summary

I hope this article is useful for all students and beginners. If you have any suggestion related to this article, then please contact me.

Post a Comment

www.CodeNirvana.in

Protected by Copyscape Online Copyright Protection
Copyright © Compilemode