Click here to Skip to main content
15,074,668 members
Articles / Web Development / ASP.NET
Posted 8 Aug 2010


51 bookmarked

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
Implementing high speed wcf service with Cient side Service Caching and Instance Management


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:



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.


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.

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 == 
//If the channel is faulted. The existing channel will be removed from the cache
//and recreate the channel again.
_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);
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.

static void ServiceFactory_Faulted(object sender, EventArgs e)
Type typ = ((ICommunicationObject)sender).GetType();
lock (_channels.SyncRoot)
((ICommunicationObject)sender).Faulted -= new EventHandler(ServiceFactory_Faulted);

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


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.


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


About the Author

Justin Mathew @ Investment Bank
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)
SQL Server
Optimization in all of these

and more @

Comments and Discussions

QuestionPerfect! Pin
Cleveland Mark Blakemore6-May-13 17:44
MemberCleveland Mark Blakemore6-May-13 17:44 
GeneralMy vote of 5 Pin
ashokinnova8-Nov-12 4:59
Memberashokinnova8-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
MemberSushant Joshi8-Sep-10 17:56 
GeneralRe: Great article! Pin
Justin Mathew @ Investment Bank8-Sep-10 22:48
MemberJustin Mathew @ Investment Bank8-Sep-10 22:48 
GeneralMy vote of 5 Pin
P S Sreejith17-Aug-10 23:17
MemberP S Sreejith17-Aug-10 23:17 
GeneralRe: My vote of 5 Pin
Justin Mathew @ Investment Bank16-Sep-10 1:49
MemberJustin 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
MemberJustin Mathew @ Investment Bank16-Sep-10 1:50 
GeneralFaulted EventHandler Unregistration Pin
Krishnan Srinivasan10-Aug-10 1:44
MemberKrishnan Srinivasan10-Aug-10 1:44 
I haven't got chance to download the solution yet. But from what I saw from the source listing, it appeared to me that the EventHandler that you had registered with the Channel's Faulted event is not unregistered before the Channel is removed from the Pool. This could cause a potential memory leak. Please check it out.
GeneralRe: Faulted EventHandler Unregistration Pin
Justin Mathew @ Investment Bank11-Aug-10 10:52
MemberJustin Mathew @ Investment Bank11-Aug-10 10:52 
GeneralRe: Faulted EventHandler Unregistration Pin
Krishnan Srinivasan11-Aug-10 22:26
MemberKrishnan Srinivasan11-Aug-10 22:26 
GeneralRe: Faulted EventHandler Unregistration Pin
Justin Mathew @ Investment Bank17-Aug-10 1:24
MemberJustin Mathew @ Investment Bank17-Aug-10 1:24 
GeneralMy vote of 5 Pin
Member38623059-Aug-10 20:17
MemberMember38623059-Aug-10 20:17 
GeneralRe: My vote of 5 Pin
Justin Mathew @ Investment Bank16-Sep-10 1:50
MemberJustin Mathew @ Investment Bank16-Sep-10 1:50 
GeneralVery nice article Pin
Ajith Cyriac9-Aug-10 13:06
MemberAjith 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.