Windows Communication Foundation Overview

WCF

WCF is provided by Microsoft .NET Framework and is used to build service, service configuration and deploy the service. WCF is a feature of Web Service, Remote service, COM and MSMQ (Microsoft Message Queuing) service. WCF supports multiple languages and multiple platforms.



Figure 1: WCF

In WCF, data communication between two service endpoint as an xml format and a binary format is shown in the following figure:



Figure 2: Different Platform Application Communication

In this figure you can see different types of service endpoint communication in xml binary format using HTTP protocol and TCP protocol.
WCF service is useful for data supply, real time communication, data presentation, monitoring traffic and report, many secure business level transactions and security.

It provides better security and service compared to Web Services.

WCF Features

  • Host via IIS.
  • Provides windows activation and windows services.
  • Provides better security services.
  • Also provides messaging and transaction service.
  • Access via protocols: HTTP, MSMQ, TCP, Peer 2 Peer, PIPES.
  • It is represented via [OperationContract] and [ServiceContract].
  • Use System.Runtime.Serialization namespace method to data serialization.
  • Provides Matadata service.
  • It does not return unhandled Exception but provides error handling configuration setting.
  • Provides better performance.
  • It can be serialized Hash table and interface.
  • It has support for different type of bindings.

WCF: Endpoint

All communications with a Windows Communication Foundation (WCF) service exists by the endpoints of the service. Endpoints provide clients access to the functionality that is offered by a WCF service (MSDN).

Each Endpoint is a portal for communicating with the world.



Figure 3: WCF End Points

Endpoint maintained in the project Web.Config file as in the following code snippet:

  1. <endpoint address="http://localhost:8080/Service/Service.svc" contract="IService"  
  2. binding="basicHttpBinding" /> 
Endpoint has the following three different parts.

Trick to remember it as ABC.

A - Address (Where),
B- Binding (How),
C- Contract (What)


Address

An address provides particular URL.

WCF support HTTP, MSMQ, TCP, P2P, PIPES.

Example

Type1: http://localhost:8080
Type2: http://localhost:8080/application.com

An address is divided in the following three parts:

Protocol: Http.
Location and Port: localhost:8080.
Service and path: application.com.

Binding

Binding means how users or clients will communication with service. It must need one transport protocol to communicate. 

Basic Http Binding

This is a basic type of communication binding. In this use HTTP protocol an by default this binding does not provide any type of security. In this type, binding use asmx services for communication.

TCP Binding

This type of binding is used to cross communicate for internet and is considered the most effective binding. This binding is used for multiple service communication. In this provide windows security and authentication and in that the messages are passed in binary format. TCP binding provide secure and safe communication for cross communication.

Peer to Peer Binding

Peer to peer binding mostly used for to data transportation between the two service points. So this type of binding is effective to develop message services.

MSMQ Binding

MSMQ binding is provided for store and forward mechanism and message passing in a queue at the operating system. Also in this the most challenging part is security.

You can see many types of binding on MSDN: Click here

Contracts

Contract is a work between the following two different points: Service and a Client.

In Windows Communication Foundation, Contracts are categorized as behavioral or structural.

WCF defines four types of contracts


Service Contract

Service contract represents the operations or methods that are available on the service endpoint. Service Contract has the following two types:

Service Contract: define to Interface.

Operation Contract: define to the method inside interface.
  1. [ServiceContract]  
  2. public interface IEmployeeService  
  3. {  
  4.     [OperationContract]  
  5.     String GetEmployeeName(int empId);  
  6. }  
Data Contract

Data contract work between service and client. It is representing to data exchanged between two points. This data is transportation to binary to xml or xml to binary one point to any other point.

The following two types of data contracts are available:
  1. Data Contract: define to a class.
  2. Data Member: define to a properties.
  1. DataContract]  
  2. public class EmployeeInfo  
  3. {  
  4.     [DataMember]  
  5.     Public string EmpName;  
  6. }
Message Contract

WCF provide default SOAP message service between service and client. A Message Contract is used to control the structure of a message body and serialization process. It is also used to send or access information in SOAP.

Fault Contract

Fault Contact is use to service’s operation.

WCF Behavior

WCF behaviour can be shared across all services or endpoints running on a machine.
  1. <system.serviceModel>  
  2.     <behaviors>  
  3.         <serviceBehaviors>  
  4.             <behavior name="DemoServiceBehavior">  
  5.                 <serviceMetadata httpsGetEnabled="True"/>  
  6.                 <serviceDebug includeExceptionDetailInFaults="False"/>  
  7.                 <serviceThrottling maxConcurrentCalls="10" maxConcurrentInstances="2" maxConcurrentSessions="1"/>  
  8.             </behavior>  
  9.         </serviceBehaviors>  
  10.     </behaviors>  
  11. </system.serviceModel>
Service behaviors affect only service related phase. Endpoint behaviors affect only endpoint related properties and operation-level behaviors. Service behavior defined at Service level, not at the endpoint level like Binding configuration.

WCF Channel

Channel is used for client and service communication via messages.

Channel has the following two different types:
  1. Transport Channel

    Handles sending and receiving message from network. Protocols like HTTP, TCP name pipes and MSMQ.

  2. Protocol Channel

    Implements SOAP based protocol.

    MORE Detail: Click here by debugmode.net.

WCF Client

WCF Client is an application that can communicate with WCF Service operations as method. A client application is a managed application that uses a WCF client to communicate with another application.

  1. Create a WCF Client.
  2. Configure a WCF Client.
  3. Use a WCF Client.

WCF Hosting

A WCF service can be hosted in the following ways:

  1. IIS hosting

    IIS hosting means Hosting in Internet Information Services (IIS). IIS is an easy method of hosting. IIS hosting only supports HTTP protocol. It is starts automatically with client request and is a straight forward process. WCF services like an ASP.NET website provides all the features of having multiple request handling and dynamic compilation to a WCF service out of the box.

  2. Self hosting

    Self hosting means Hosting in a Console or Desktop application. It is a providing us full control.
      
  3. WAS hosting

    WAS hosting means Windows Activation Services. It is available in IIS7.0 and it is support HTTP, TCP and Named pipes.

  4. WS hosting

    WS hosting means Windows Service hosting. This is same as hosting the service in IIS without message activated. This type service host when system is start.

Metadata

The Windows Communication Foundation (WCF) provides an infrastructure for exporting, publishing, retrieving, and importing service metadata. WCF services use metadata to represent how to interact with the services endpoints and automatically generate client code for accessing the service endpoint. Metadata is generating Service Endpoint instances.

Instance management

WCF Instance Management work with client request and provides the following three types of  Instances.

Per-Call instance mode



Figure 4: WCF Per-Call instances

  • If service is stateless.
  • If service has light-weight initialization code.
  • If service is single threaded.

Per-Session instance mode



Figure 5: WCF Pre-Session instances

  • Pre-session is a private session and works with each client request.
  • The same WCF service instance waits for the method call.
  • When the client finishes its activity, the WCF instance is destroyed and served to the garbage collector for clean up.
  • If service needs to maintain some state between calls from the same client.
  • When the service start and close then the session endpoint configures.

Single Instance Mode



Figure 6: WCF Single Instances

  • If your service needs to maintain some state between clients then use it.
  • When client request work independently then use it.
  • In this create only one Instance to all the client requests.
  • This type service create only one at a time with service hosted.

Instance Deactivation



Figure 7: WCF instances and Contexts

The service instance is basically hosted in a context. Session is actually related ti the client message not to the instance of the service, but session is related to the context of the service host. While creating new context the session gets start and when the context expires then the session terminates. Its context is called Instance Deactivation.

The ReleaseInstanceMode property of the OberationalBehavior attribute used to control the context deactivation of the service instance.

Four type Release mode available as following,

  • RealeaseInstanceMode.None.
  • RealeaseInstanceMode.BeforeCall.
  • RealeaseInstanceMode.AfterCall.
  • RealeaseInstanceMode.BeforeAndAfterCall.

Durable Service

Durable service provides the instance to multiple requests. This service is also providing restore state object request. In this service the state information stores in the database. This type service basically works with the database and through this service data gets saved into the database with a unique key and also this type of service data corresponding with Globally Unique Identifier.

  1. [Serializable]  
  2. [DurableService()]  
  3. public class MyService : IMyservice  
  4. {  
  5.     [DurableOperation(CanCreateInstance = true)]  
  6.     public int StartPersistance()  
  7. {  
  8. //write Some Code  
  9. }  
  10. [DurableOperation(CompletesInstance = true)]  
  11. public void EndPersistence()  
  12. {  
  13.     //Write Some Code  
  14. }  
  15. }  
Throttling

WCF throttling provides some properties that you can use to limit how many instances or sessions are created at the application level. WCF will automatically place the pending callers in a queue and serve them out of the queue in order. If the clients call timeout expires while pending in the queue, the client will get a TimeoutException. It can be configured at administrative or programmatical level.

Throttling provide three type attribute.
  • maxConcurrentCalls.
  • maxConcurrentInstances.
  • maxConcurrentSession.

Attribute in configure file

  1. <behaviors>  
  2.     <serviceBehaviors>  
  3.         <behavior name = "ThrottledBehavior">  
  4.             <serviceThrottling  
  5. maxConcurrentCalls = "10"  
  6. maxConcurrentSessions = "32"  
  7. maxConcurrentInstances = "66" />  
  8.         </behavior>  
  9.     </serviceBehaviors>  
  10. </behaviors>
WCF Operation

The following three types of operations are available in WCF as in the following figure:

One Way Operation



Figure 8: One Way Mode

In one way mode, the user sends to the server. It does not effect request success or fail.
  1. [ServiceContract]  
  2. public interface IMyService  
  3. {  
  4.     [OperationContract(IsOneWay=true)]  
  5.     void MyMethod(EmpDetails emp);  
  6. }
Request and Response (Two Way) operation



Figure 9: WCF Request and Response

By default this operation work with client request and WCF service response. In this mode user send it to request server and will be waiting to receive response from the service.

WCF Call Back Service

WCF can provide the following two types of callback Service.
  • Client side Callback

    In callback operation client will work as service and service will work as client. That means this type service expose callback endpoint to client and service. 

  • Service Side Callback

    This type of callback service work for maintaining client side endpoint and message. Also work with client side callback instance. In this type, the service can be used or context class and callback channel.

WCF Security

Windows Communication Foundation (WCF) is a secure, reliable and scalable messaging platform. It provides message transportation between the two points for security. WCF supports transport protocols such as  TCP, HTTP, MSMQ, etc. These type of securities do not disturb any messaging system. It provides many ways such as windows authentication, username and password, roles based security. Also create custom authorization policy.

Authentication

  • Windows
  • Username
  • Certificate

Authorization

  • Role Based.
  • Identity Based.
  • Resource based.

Transfer Security

  • Point to point.
  • Streaming.
  • Binding limitation.

I hope you liked this WCF article and got some idea about WCF.


Similar Articles