Click here to Skip to main content
Click here to Skip to main content

WCF Optimization and Service @ High Speed

, 11 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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.

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

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)

Share

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)
ASP.Net MVC and MVVM
JASON
JQuery
SQL Server
Oracle
Optimization in all of these
 
and more @ http://jetmathew.wordpress.com/
Follow on   Twitter

Comments and Discussions

 
QuestionPerfect! PinmemberCleveland Mark Blakemore6-May-13 18:44 
GeneralMy vote of 5 Pinmemberashokinnova8-Nov-12 5:59 
GeneralMy vote of 5 Pinmembermanoj kumar choubey16-Apr-12 4:20 
GeneralGreat article! PinmemberSushant Joshi8-Sep-10 18:56 
GeneralRe: Great article! PinmemberJustin Mathew @ British Standard Institute (BSi)8-Sep-10 23:48 
GeneralMy vote of 5 PinmemberP S Sreejith18-Aug-10 0:17 
GeneralRe: My vote of 5 PinmemberJustin Mathew @ British Standard Institute (BSi)16-Sep-10 2:49 
GeneralMy vote of 5 Pinmemberammavan11-Aug-10 20:10 
GeneralRe: My vote of 5 PinmemberJustin Mathew @ British Standard Institute (BSi)16-Sep-10 2:50 
GeneralFaulted EventHandler Unregistration PinmemberKrishnan Srinivasan10-Aug-10 2:44 
GeneralRe: Faulted EventHandler Unregistration PinmemberJustin Mathew From British Standard Institute11-Aug-10 11:52 
GeneralRe: Faulted EventHandler Unregistration PinmemberKrishnan Srinivasan11-Aug-10 23:26 
GeneralRe: Faulted EventHandler Unregistration PinmemberJustin Mathew @ British Standard Institute (BSi)17-Aug-10 2:24 
GeneralMy vote of 5 PinmemberMember 38623059-Aug-10 21:17 
GeneralRe: My vote of 5 PinmemberJustin Mathew @ British Standard Institute (BSi)16-Sep-10 2:50 
GeneralVery nice article PinmemberAjith Cyriac9-Aug-10 14:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 11 Aug 2010
Article Copyright 2010 by Justin Mathew @ Investment Bank
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid