Thursday 13 February 2014

Introduction to WCF

What is Windows Communication Foundation?

WCF stands for Windows Communication Foundation. It is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is Microsoft's latest technology that enables applications in a distributed environment to communicate with each other. WCF is Microsoft's unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing. In Modern Application (Distributed Application) development, we use COM+, .NET Enterprise Services, MSMQ, .NET Remoting, Web services, etc. for communication. All these technologies play a different role and to use it we need to develop different solutions for different technologies. we have to focus on each of the technologies to develop rather than the application business logic. WCF unifies the capabilities into single, common, general service oriented programming model for Communication.WCF provides a common approach using a common API which developers can focus on their application rather than on communication protocol. So WCF is a combined feature of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.

Why Do We Need WCF?
Suppose we have one main Bank system which is directly connected with the main database of bank, that provides other systems like ATM machine, Loan System data using various communication protocols like Remoting , web service, etc. For communicating with different Systems using different communication protocols, we will have to develop the different system with the help of different API of that technology or we can just make a common system with the help of WCF. In WCF, we have to just make different End points for different services. There is no need to learn a different API. we can use only one common API for communication with different System.In WCF, we have to just make different End points for different services. There is no need to learn a different API. You can use only one common API for communication with a different System.
There is many more advantages given below, thats why we use WCF:-

Advantages of WCF
1) WCF can maintain transaction like COM+ Does
2) It can maintain state
3) It can control concurrency
4) It can be hosted on IIS, WAS, Self hosting, Windows services
5) It has AJAX Integration and JSON (JavaScript object notation)    support
6) In WCF, there is no need to make much change in code for          implementing the security model and    changing the binding.      Small changes in the configuration will make your                requirements.
7) WCF has integrated logging mechanism, changing the                configuration file settings will provide this functionality.      In other technology developer has to write the code.

Difference between WCF and Web service

WCF
ASP.NET WEB SERVICES
[Service Contract ] and [Operation Contract] attributes define web service and methods

[Service Contract]

Public interface iTest
{
    [Operation Contract]

     String Showmessage();
}
Public class Service : iTest
{
     Public string showmessage()
     {
          Return “ WCF Example by Rakesh”;
      }
}

[Web Service] and [Web Method] attributes define web service and methods

[Web Service]

Public class service:

System .Web .Services.Webservice
{
    [Web Method]

    Public string Test()
    {

          Return “ web service by Rakesh”;
      }
}
Hosted in IIS, WAS (Windows Activation Services),  Self -hosting, Windows Services

Hosted in IIs.
Accessed through HTTP, TCP , MSMQ , P2P , Named Pipes.

Accessed through HTTP.
Supports security, Reliable messaging, transactions, durable messages, Service orientation, service metadata, AJAX and REST support, extensibility.
Supports security service
Uses the Service Metadata tool (svcutil.exe) to generate the client for the service.
Uses the command-line tool WSDL.EXE to generate the client for the service.
Unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.
Unhandled exceptions are returned to the client as SOAP faults.
The generated WSDL can customize by using Service Metadata Behavior class.
The generated WSDL can customized by using Service Description Format Extension class.
System.RunTime.Serialization is supported.
Better performance.
DataContract Attribute and DataMember Attribute can be added to .NET Framework types to indicate that instances of the type are to be serialized into XML, and which particular fields or properties of the type are to be serialized.
Hash table can be serialized

System.XML. Serialization is supported.
Worse performance
Only public fields or properties of .NET types can be translated into XML.
Only the classes which implement IEnumerable and Icollection interface can be serialized.
Has table can not be serialized
Supports different type of bindings like BasicHttpBinding, WSHttpBinding, WSDualHttpBinding etc.
Only Used SOAP or XML for this.

A WCF Service is composed of three components parts viz,

1) Service Class - A WCF service class implements some service as a set of methods.

2) Host Environment - A Host environment can be a Console application or a Windows Service or a Windows Forms application or IIS as in case of the normal asmx web service in .NET.

3) Endpoints - 
 An endpoint is basically a resource or an entity in the network to which messages can be sent. Endpoints are responsible for the communication between a client and the service. Elements of endpoints are:
  • Addresses to locate the service
  • Bindings to communicate with the service
  • Contracts to specify the functionalities to be performed while communicating with the service.
  • Endpoint behaviors to specify the runtime behavior of the service endpoint.
These Addresses, Bindings and Contracts are normally referred to as ABC's of WCF.


Addresses: In order to send a message to a service, you would need its address. An address in WCF would consist of the following items:
  • Scheme: Basically the transport protocol used.
  • Machine: Full Domain Name of the machine.
  • Port: Defines the port number where the service is running.
  • Path: Path of the service.
Example can be written as:
  • scheme://domain_name:port/path
  • http://localhost:8080/services/MyFirstService
In the above example, a service is locally hosted with HTTP being the transport protocol and 8080 being the port used, path being services/MyFirstService.

Binding:   The endpoints also define a binding that specifies how a client will communicate with the service and the address where the endpoint is hoste.We can also define our own custom binding in WCF to fulfill our need. All built in bindings are defined in the System.ServiceModel Namespace.
Below is the list of pre-defined binding elements: 
  • basicHttpBinding
  • wsHttpBinding
  • wsDualHttpBinding
  • wsFederationHttpBinding
  • netTcpBinding
  • netNamedPipeBinding
  • netMsmqBinding
  • msmqIntegrationBinding
  • netPeerTcpBindin
Webconfig file where binding is defined.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="MyFirstService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MyFirstservice"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="MyFirstService">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>
d
Contract Agreements between the client and services. A Contract that defines which methods of the Service class will be accessible via the endpoint.


Different contracts in WCF

Service Contract

Service contracts describe the operation that service can provide. For Eg, a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract. It will be created using Service and Operational Contract attribute.

Data Contract

Data contract describes the custom data type which is exposed to the client. This defines the data types, which are passed to and from service. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or data types cannot be identified by the client e.g. Employee data type. By using DataContract we can make client to be aware of Employee data type that are returning or passing parameter to the method.

Message Contract

Default SOAP message format is provided by the WCF runtime 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.

Fault Contract

Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault Contract. Fault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.






No comments:

Post a Comment