Archive for February, 2010

Further to my previous blog entry about the WCF 10 connection limit and its inability to implement the IDisposable interface or allow the use of the “using block” to free up the connections, I decided to write a generic base class.

This base class does not close the connection automatically,nor does it dispose of the connection, but it does allow me to create and use a service client a lot easier.

Given the task of populating and binding controls within an AJAX accordion control I thought things would be easy. However, binding to a selected index of a dropdown control was not as easy as I first suspected.

Next, I had to bind the value from the DB to the status of the device and to do this I use the item data bound event of the accordion control. Firstly, I have to AccordionItemEvent being passed in is within the content part of the control, i.e. the one being edited.

Once I’ve confirmed this, I need to convert the item in the control to the Data Transfer Object being retrieved from the WCF Service. Then, if the cast to the DTO is not null. I find the control and set the value of the dropdown.

Binding an accordion item

///<summary>

/// Event handler when a new item is bound in the contentTemplate of the devices accordion.

In the code sample above it is shown that the data source for the the dropdown comes from a WCF Service which, in turn, returns a list of interfaces ILookupData. The working and casting for this service call is shown in the code sample below.

Recently, I had a problem with an AJAX enabled application which was calling a WCF service. As can be seen below, in my calls to the Service, I used the “using statement” to allow the connection to the service to be closed after use.

Code Snippet

///<summary>

/// Gets the towns.

///</summary>

///<returns>The list from the cache if it exists; or from the database</returns>

publicList<TownDto> GetTowns()

{

if (!this.cacheManager.Contains("towns"))

{

using (var svc = newNetworkServiceClient())

{

var towns = svc.GetAllTowns();

this.cacheManager.Add(

"towns",

towns,

CacheItemPriority.Normal,

null,

newSlidingTime(TimeSpan.FromMinutes(Constants.CacheTimeout)));

}

}

return (List<TownDto>)this.cacheManager.GetData("towns");

}

However, as i played with the application it became apparent that the more calls its made the slower the application became. Eventually, after wading threw all the application tiers I found the culprit was: by default WCF only allows 10 connections, which meant my connections weren’t being closed by the using statement.

The code has now been refactored to close the service client explicitly. It now works!