Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET

WCF Optimization and Service @ High Speed

Rate me:
Please Sign up or sign in to vote.
4.77/5 (19 votes)
11 Aug 2010CPOL2 min read 75.5K   1.4K   51   16
Implementing high speed wcf service with Cient side Service Caching and Instance Management

Introduction

It has been a while since I was thinking of sharing some WCF service optimization techniques which we can easily implement as part of Service framework and service client side. This article talks about the following important points:

  • Client side Service Caching (Service Pooling)
  • Dynamic binding of Service
  • Object caching (Object Pooling)
  • Service instance management
  • Easy mapping Business objects in XML

There is nothing new in this article other than using C# list objects to cache services and objects from service. This includes an XML file which is easy to map objects and its types to create objects at run time using factory pattern.

The UML high level design picture shows everything explained in this article. I am sure we can do some more optimization in this project, but for the time being I just want to present a few areas where we can improve performance of Service Communication.

What is the Main Time Consuming Process in WCF Service Call?

One of the main time consuming processes in any service call or any call to the outside application boundary is initiating and opening the connection across the channel. That is where we need connection pooling. In WCF service call we can easily achieve with caching service channels at client side. This article gives you with channel caching and object caching to improve speed.

The following picture will clarify everything:

Solution

Prerequisites

You will need VS 2010 to open the Solution, but I am sure the same code will work in VS 2008 as well if you create another VS 2008 solution and add these files.

Solution Overview

If you look at the following picture, you can easily understand the different layers and files included. Probably you can download the attached code and dig into each file to get more about implementation.

Solution

Using the Code

The code below does Client side service caching. It is also tested with multi threading since multiple threads will be accessing cache collection object asynchronously.

C#
public static I CreateInstance<I>() where I : class
{
string endPointConfig = typeof(I).Name.ToString();
lock (_channels.SyncRoot)
{
if (_channels.ContainsKey(endPointConfig) == false)
{
_channels.Add(endPointConfig, OpenChannel<I>());
}
else if (((((IClientChannel)_channels[endPointConfig]).State == 
					CommunicationState.Faulted)) || 
	(((IClientChannel)_channels[endPointConfig]).State == 
					CommunicationState.Closed) || 
	(((IClientChannel)_channels[endPointConfig]).State == 
					CommunicationState.Closing))
{
//If the channel is faulted. The existing channel will be removed from the cache
//and recreate the channel again.
((IClientChannel)_channels[endPointConfig]).Abort();
((IClientChannel)_channels[endPointConfig]).Close();
_channels.Remove(endPointConfig);
_channels.Add(endPointConfig, OpenChannel<I>());
}
return _channels[endPointConfig] as I;
}
}       

This part of code is to create channel and open the channel.

private static I OpenChannel<I>() where I : class
{
string endPointConfig = typeof(I).Name.ToString();
ChannelFactory<I> factory = new ChannelFactory<I>(endPointConfig);
factory.Open();
I channel = factory.CreateChannel();
((IClientChannel)channel).Faulted += new EventHandler(ServiceFactory_Faulted);
return channel;
}

This part of code gets executed when service is faulty and the same time we need to remove from Cache collection.

C#
static void ServiceFactory_Faulted(object sender, EventArgs e)
{
((ICommunicationObject)sender).Abort();
((ICommunicationObject)sender).Close();
Type typ = ((ICommunicationObject)sender).GetType();
lock (_channels.SyncRoot)
{
((ICommunicationObject)sender).Faulted -= new EventHandler(ServiceFactory_Faulted);
_channels.Remove(typ.Name);
}
}

This code part does object pooling and is used in Service host when a service invokes a business object.

ObjectFactory.cs

C#
public static I CreateInstance<I>() where I : class
{
lock (_pooledObjects.SyncRoot)
{
if (_pooledObjects.ContainsKey(typeof(I)) == false)
{
_pooledObjects.Add(typeof(I), GetInstance<I>());
}
return _pooledObjects[typeof(I)] as I;
}
// return GetInstance<I>() as I;
}

That's All About It - Quite Simple

I hope this approach is very simple and good to implement. Please have a look and if you like this article, please leave a vote and a comment. Thanks. Any comments and questions will be appreciated.

Points of Interest

I am always concentrating to deliver high performance applications with easy customized approach. I spent some time to tune WCF service with the help of many memory profilers like ants profiler.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer British Standard Institute (BSi), London
United Kingdom United Kingdom
My passion is

Microsoft distributed and connecting Technologies like Windows Communication foundation (WCF)
Workflow Services (WWF)
ASP.Net MVC and MVVM
JASON
JQuery
SQL Server
Oracle
Optimization in all of these

and more @ http://jetmathew.wordpress.com/

Comments and Discussions

 
QuestionPerfect! Pin
Cleveland Mark Blakemore6-May-13 17:44
Cleveland Mark Blakemore6-May-13 17:44 
GeneralMy vote of 5 Pin
ashokinnova8-Nov-12 4:59
ashokinnova8-Nov-12 4:59 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey16-Apr-12 3:20
professionalManoj Kumar Choubey16-Apr-12 3:20 
GeneralGreat article! Pin
Sushant Joshi8-Sep-10 17:56
Sushant Joshi8-Sep-10 17:56 
GeneralRe: Great article! Pin
Justin Mathew @ Investment Bank8-Sep-10 22:48
Justin Mathew @ Investment Bank8-Sep-10 22:48 
GeneralMy vote of 5 Pin
P S Sreejith17-Aug-10 23:17
P S Sreejith17-Aug-10 23:17 
GeneralRe: My vote of 5 Pin
Justin Mathew @ Investment Bank16-Sep-10 1:49
Justin Mathew @ Investment Bank16-Sep-10 1:49 
GeneralMy vote of 5 Pin
DamodaranMajesh11-Aug-10 19:10
professionalDamodaranMajesh11-Aug-10 19:10 
GeneralRe: My vote of 5 Pin
Justin Mathew @ Investment Bank16-Sep-10 1:50
Justin Mathew @ Investment Bank16-Sep-10 1:50 
GeneralFaulted EventHandler Unregistration Pin
Krishnan Srinivasan10-Aug-10 1:44
Krishnan Srinivasan10-Aug-10 1:44 
GeneralRe: Faulted EventHandler Unregistration Pin
Justin Mathew @ Investment Bank11-Aug-10 10:52
Justin Mathew @ Investment Bank11-Aug-10 10:52 
GeneralRe: Faulted EventHandler Unregistration Pin
Krishnan Srinivasan11-Aug-10 22:26
Krishnan Srinivasan11-Aug-10 22:26 
GeneralRe: Faulted EventHandler Unregistration Pin
Justin Mathew @ Investment Bank17-Aug-10 1:24
Justin Mathew @ Investment Bank17-Aug-10 1:24 
GeneralMy vote of 5 Pin
Member38623059-Aug-10 20:17
Member38623059-Aug-10 20:17 
GeneralRe: My vote of 5 Pin
Justin Mathew @ Investment Bank16-Sep-10 1:50
Justin Mathew @ Investment Bank16-Sep-10 1:50 
GeneralVery nice article Pin
Ajith Cyriac9-Aug-10 13:06
Ajith Cyriac9-Aug-10 13:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.