WCF Tutorial

Post date: Mar 1, 2011 9:54:38 AM

Introduction to WCF

Windows Communication Foundation (Code named Indigo) is a programming platform and runtime system for building, configuring and deploying network-distributed services. It is the latest service oriented technology; Interoperability is the fundamental characteristics of WCF. It is unified programming model provided in .Net Framework 3.0. WCF is a combined features of Web Service, Remoting, MSMQ and COM+. WCF provides a common platform for all .NET communication.

Below figures shows the different technology combined to form WCF.

 

Advantage

Disadvantage

Making right design for your requirement is little bit difficult. I will try to help you on solving these difficulties in the following article.

Fundamental

EndPoint

WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal for communicating with the world.

All the WCF communications are take place through end point. End point consists of three components.

Address

Basically URL, specifies where this WCF service is hosted .Client will use this url to connect to the service. e.g

http://localhost:8090/MyService/SimpleCalculator.svc

Binding

Binding will describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.

A binding has several characteristics, including the following:

The following table gives some list of protocols supported by WCF binding.

Contract

Collection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface will be mentioned in the Contract, so the client application will be aware of the operations which are exposed to the client. Each operation is a simple exchange pattern such as one-way, duplex and request/reply.

Below figure illustrate the functions of Endpoint

Example:

Endpoints will be mentioned in the web.config file on the created service.

<system.serviceModel> <services>       <service name="MathService"         behaviorConfiguration="MathServiceBehavior">        <endpoint          address="http://localhost:8090/MyService/MathService.svc" contract="IMathService"           binding="wsHttpBinding"/>        </service>     </services>     <behaviors>       <serviceBehaviors>         <behavior name="MathServiceBehavior">           <serviceMetadata httpGetEnabled="True"/>           <serviceDebug includeExceptionDetailInFaults="true" />         </behavior>       </serviceBehaviors>     </behaviors>   </system.serviceModel> 

Binding and Behavior

Binding

Simple definition for Binding describes how the client will communicate with service. We can understand with an example.

 

Consider a scenario say, I am creating a service that has to be used by two type of client. One of the client will access

SOAP using http and other client will access Binary using TCP. How it can be done? With Web service it is very difficult

to achieve, but in WCF its just we need to add extra endpoint in the configuration file.

 

<system.serviceModel>     <services>       <service name="MathService"         behaviorConfiguration="MathServiceBehavior">       <endpoint address="http://localhost:8090/MyService/MathService.svc"          contract="IMathService"           binding="wsHttpBinding"/>       <endpoint address="net.tcp://localhost:8080/MyService/MathService.svc"          contract="IMathService"           binding="netTcpBinding"/>        </service>     </services>     <behaviors>       <serviceBehaviors>         <behavior name="MathServiceBehavior">           <serviceMetadata httpGetEnabled="True"/>           <serviceDebug includeExceptionDetailInFaults="true" />         </behavior>       </serviceBehaviors>     </behaviors>   </system.serviceModel>  

See how simple it is in WCF. Microsoft is making everything simple.

 

coding to its scope:

Example:

In the below configuration information, I have mentioned the Behavior at Service level. In the service behavior I have mention the servieMetadata node with attribute httGetEnabled='true'. This attribute will specifies the publication of the service metadata. Similarly we can add more behavior to the service.

 

<system.serviceModel>

<services>

<service name="MathService"

behaviorConfiguration="MathServiceBehavior">

<endpoint address="" contract="IMathService"

binding="wsHttpBinding"/>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior name="MathServiceBehavior">

<serviceMetadata httpGetEnabled="True"/>

<serviceDebug includeExceptionDetailInFaults="true" />

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

 

Contracts and Service Host

Contracts

In WCF, all services are exposed as contracts. Contract is a platform-neutral and standard way of describing what the service does. Mainly there are four types of contracts available 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, that 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.

Service Host

Service Host object is in the process of hosting the WCF service and registering endpoints. It loads the service configuration endpoints, apply the settings and start the listeners to handle the incoming request. System.ServiceModel.ServiceHost namespace hold this object. This object is created while self hosting the WCF service.

In the below example you can find that WCF service is self hosted using console application.

//Creating uri for the hosting the service Uri uri = new Uri("http://localhost/CategoryService"); //Creating the host object for MathService   ServiceHost host = new ServiceHost(typeof(CategoryService), uri); //Adding endpoint to the Host object   host.AddServiceEndpoint(typeof(ICategoryService),new WSHttpBinding(), uri);   host.Open(); //Hosting the Service   Console.WriteLine("Waiting for client invocations");   Console.ReadLine();   host.Close();

Message and Channel

Message:

WCF Message is the unit of data exchange between client and service. It consists of several parts, including a body and headers.

WCF Runtime:

WCF runtime is the set of object responsible for sending and receiving message. For example formatting the message, applying security and transmitting and receiving message using various protocol.

Channels:

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.

WCF Client and Metadata

WCF Client

WCF client is a client application creates to expose the service operations as method. Any application can host a WCF client, including an application that host a service. Therefore it is possible to create a service that includes WCF clients of other services.

A client application is a managed application that uses a WCF client to communicate with another application. To create a client application for a WCF service requires the following steps:

Metadata

Characteristics of the service are described by the metadata. This metadata can be exposed to the client to understand the communication with service. Metadata can be set in the service by enabling the ServiceMetadata node inside the servcieBehaviour node of the service configuration file.

<system.serviceModel>     <services>       <service name="MathService"         behaviorConfiguration="MathServiceBehavior">         <endpoint address="" contract="IMathService"           binding="wsHttpBinding"/>       </service>     </services>     <behaviors>       <serviceBehaviors>         <behavior name="MathServiceBehavior">     <serviceMetadata httpGetEnabled="True"/>           <serviceDebug includeExceptionDetailInFaults="true" />         </behavior>       </serviceBehaviors>     </behaviors>   </system.serviceModel> 

This metadata can be viewed while creating WCF client application using SvcUtil.exe

WCF Architecture:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Contracts

Contracts layer are next to that of Application layer. Developer will directly use this contract to develop the service. We are also going to do the same now. Let us see briefly what these contracts will do for us and we will also know that WCF is working on message system.

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.

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