Getting Started With WCF Service

WCF stands for windows communication platform which is used to communicate with cross Platform application and to develop network distributed application.
Previously WCF was knows Indigo service and later on with .Net Framework its knows 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 single platform without using different service for different requirement.

Let us understand with following Diagram


From above diagram, I am trying to give you 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 the wide range of feature including communication protocol support, Security and so on let us considers the following example.

This is normal communication of Web service and client in which client requirement is to exchange the data using HTTP Protocol.
In the above example Web service communicate with client using HTPP protocol and only HTTP protocol supports web service and suppose in future client wants to exchange data using TCP as wel as HTTP without creating a separate service then with Web service its 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 wide range of communication protocol such as HTTP ,TCP and so on.

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

Contracts in WCF

Contracts are useful to built the WCF service application .Contracts defines what protocol (binding) does service use ,how communication will be done ,what message exchange format to use and much.
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
Service contracts nothing but the 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
Operation contract defines the method which is exposed to the client to exchange the information between client and server.Operation contract describes the 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 same as get set properties but 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 error which is occur in WCF service, basically when we develop 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, what went wrong? Not on how and where cause the error.
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 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 service, protocol and encoding used for binding
Service Run-time in WCF
It contains the behaviors that occur during run-time of service.The following behaviors managed by service run-time layer.
  1. Throttling Behavior
  2. Error Behavior 
  3. Metadata Behavior
  4. Instance Behavior 
  5. Transaction Behavior
  6. Dispatch Behavior 
  7. Concurrency Behavior
  8. Parameter filtering
  9. Message inspection
  • 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.
  • Concurrency Behavior-Its 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-Its used to inspect specific part or all parts of  the message through service.
 Messaging in WCF Service
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
  1. Transport Channels
  2. Protocol Channels  
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 in WCF

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) 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
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)
Advantages of WCF
  1. Support for different protocol
  2. Security over transport as well as Message level
  3. Easy to implement MSMQ and REST Service.
  4. Load balancing & support scaling.
  5. It can control concurrency.
  6. It can be hosted on IIS, WAS, Self hosting, Windows services.
  7. Multiple Message Patterns.
  8. 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
Copyright © Compilemode