Posts Tagged ‘C#’

Performance tip – Caching expiry in web applications

Caching data that rarely change can give you a big performance boost especially if it takes time to fetch and/or calculate that data. In .NET 4.0, Microsoft introduced the .NET Memory Cache which is a thread safe cache that can be used Windows and Web applications. For more information about the MemoryCache class you can visit

While I was doing a load test for web application for one of my clients, I noticed that the Queued Requests performance counter goes up every five minutes. By using a code profiler, it showed that there were many calls to refresh some of the cached data. Taking a closer look at the code below, if user 1 executing line (value = service.GetStreets();) while the other users are checking if value is cached, there is a potential of thousands of requests to fetch the same data especially if that data is frequently used.

We were using a CacheWrapper that applies a cache policy based on the cache name. With the CacheWrapper we can write the code to cache the Street Streets as follows:

public List<Street> GetStreets()

var value = CacheWrapper.Get(CacheNames.Streets) as List<Street>;

if (value == null)

    value = service.GetStreet();
    CacheWrapper.Set(CacheNames.Streets, value);


return value;


My first approach was to use a lock and double check the value

bool StreetsDataInitialized = false;
Object StreetsLock = new Object();

public List<Street> GetStreets()

var value = CacheWrapper.Get(CacheNames.Streets) as List<Street>;

if (value == null)
StreetsDataInitialized = false;

lock (StreetsLock)
 if (StreetsDataInitialized)
    return CacheWrapper.Get(CacheNames.Streets) as List<Street>;

 value = service.GetStreets();
 CacheWrapper.Set(CacheNames.Streets, value);
 StreetsDataInitialized = true;



return value;


But there were many values that need to be cached and refreshed and I didn’t want to create two variables per cached value. I came up with a more generic way to achieve my goal. I started with the CacheLock Class

 public class CacheLock
     public bool Initialized { get; set; }
     public object LockObject = new object();


I used the ConcurrentDictionary to store the locks. I also created a generic GetOrSetCacheValue function which gets or creates a CacheLock for each cached data and invoke “function” if the data is not cached.

Public class CacheableLookupProvider
private static ConcurrentDictionary<string, CacheLock> LockDictionary = new ConcurrentDictionary<string, CacheLock>();
private readonly IStreetsService service;

Public CacheableStreetsProvider(IStreetsService service)
    this.service = service;

 private T GetOrSetCachedValue<T>(string cacheName, Func<T> function) where T : class
    var value = CacheWrapper.Get(cacheName) as T;
    if (value == null)
       var lockObject = LockDictionary.GetOrAdd(cacheName, new CacheLock());
       lockObject.Initialized = false;
      lock (lockObject.LockObject)
         if (lockObject.Initialized)
           return CacheWrapper.Get(cacheName) as T;
         value = function();
         CacheWrapper.Set(cacheName, value);
         lockObject.Initialized = true;
    return value;

 public List<Street> GetStreets ()
    return GetOrSetCachedValue(CacheNames.Streets, () => service.GetStreets());

 public List<Province> GetProvinces ()
   return GetOrSetCachedValue(CacheNames.Provinces, () => service. GetProvinces());


Categories: Tags: , , ,

Read Authorization section from Web.config

In my current project, I had to read the find the Windows group for the ESB portal which can be found in the authorization section of the web.config for the ESB portal. I used the following code to read it:

var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(“/Bam”, “Default web Site”);
var section = config.GetSection(“system.web/authorization”) as AuthorizationSection;

if (section == null)
    return string.Empty;

foreach (AuthorizationRule rule in section.Rules)
     if (rule.Action.ToString().ToLower() == “allow” && rule.Roles.Count > 0)
        foreach (var item in rule.Roles)
            if (item != “*”)
                return rule.Roles[0];
return string.Empty;


Categories: .NET,, C#, Tips Tags: , ,

TFS2010: Retrieve Associated Workitems to Changesets TFS API

How to Retrieve Workitems Associated to Changesets in TFS using C# and

You need to add the following references




They are located at “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\” on my Windows 7 instance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace ConsoleApplication2
    class Program
        static void Main(string[] args)
            var changeset = GetChangeset(new Uri("http://win7-pc:8080/tfs/defaultcollection"), 21);
            foreach (var w in changeset.WorkItems)
                Console.WriteLine("WorkItemId:" + w.Id);
                Console.WriteLine("WorkItemTitle:" + w.Title);


        private static Changeset GetChangeset(Uri serverUri, int changesetId)
            var tfs = new TfsTeamProjectCollection(serverUri);
            var svc = tfs.GetService<VersionControlServer>();
            var changeset = svc.GetChangeset(changesetId);

            return changeset;


Imports Microsoft.TeamFoundation.VersionControl.Client
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Module Module1

    Sub Main()

        Dim changeset As Changeset = GetChangeset(New Uri("http://win7-pc:8080/tfs/defaultcollection"), 21)
        For Each w As WorkItem In changeset.WorkItems
            Console.WriteLine("WorkItemId:" & w.Id)
            Console.WriteLine("WorkItemTitle:" & w.Title)


    End Sub

    Function GetChangeset(ByVal serverUri As Uri, ByVal changesetId As Integer) As Changeset
        Dim tfs As TfsTeamProjectCollection = New TfsTeamProjectCollection(serverUri)
        Dim svc = tfs.GetService(Of VersionControlServer)()
        Dim changeset As Changeset = svc.GetChangeset(changesetId)

        Return changeset
    End Function
End Module

Categories: .NET, C#, TFS, TFS2010, VB Tags: , , ,

WCF Data Service Exception: The LoadAsync method cannot be called when the DataServiceCollection is not a child collection of a related entity

In code you most likely called the no argument LoadAsync() method. Make sure you call pass the correcgt URI to the LoadAsync method. For example: if you service is located at http://localhost:1033/Services/PersonInfoService.svc/People

Make sure you pass the service URI without /People to the service context and pass /People to to the LoadAsync method

var people = new PersonEntities(new Uri("http://localhost:1033/Services/PersonInfoService.svc/"));
var Items = new DataServiceCollection<Person>(people);
//Pass the collection path to the LoadAsync method
Items.LoadAsync(new Uri("/People", UriKind.Relative));
Categories: .NET, C#, OData, Services, WCF Tags: , , ,

Use Red Gate SmartAssembly in Automated Team Build to Obfuscate Assemblies

This post is in response to a question asked in the TFS Automated Build Forum:

Smart Assembly is a product to obfuscate .NET assemblies and automated error reporting. For more information about the tool, you can visit

To answer the question in the MSDN forum, I downloaded and installed the trial version Smart Assembly from Red Gate’s website. There wasn’t anything tricky while installing the product and it was straight forward.

I had to create a smart assembly project (.saproj) file which is an xml file that contains all the features that need to be enabled and their configuration. Smart Assembly comes with its configurator which makes it really easy to configure the features to be used. The following is a screen shot of the configurator

Follow these steps to create a project

  • Click New Project
  • Click Browse Assembly button
  • Select the file to be obfuscated, you want to select the dll or exe file located in the bin\release folder

  • Click Set Destination button, the configurator doesn’t allow you to select the same destination file as the input one using the same folder (i.e. over write the input one)

  • I have set the destination to be bin\SmartAssembly\SmartAssemblyPOC.exe
  • Notice the toolbar, clicking on toolbar buttons auto scrolls to the respective section

  • Check the assembly that you want to obfuscate
  • Click Save project on the left list
  • Save the saproj at the same directory where the project is saved. You can choose other location if you want to
  • Test your project by clicking Build on the left

Usually we don’t have one project in a solution. Therefore it is not practical to open the smart assembly tool for every project and build it manually. Luckily, we can integrate building the smart assembly project while building the project using MSBuild.

I basically followed the instruction in this link;

  • Right click on your project
  • Unload the project
  • Find the following line if you have a vb project

<Import Project=$(MSBuildToolsPath)\Microsoft.VisualBasic.targets />

  • Or this line if you are using C#

<Import Project=$(MSBuildToolsPath)\Microsoft.CSharp.targets />

  • Below the above line add the Smart Assembly targets

<UsingTask TaskName=SmartAssembly.MSBuild.Tasks.Build
AssemblyName=SmartAssembly.MSBuild.Tasks, Version=, Culture=neutral, PublicKeyToken=7f465a1c156d4d57 />

<Target Name=AfterBuild
Condition= ‘$(Configuration)’ == ‘Release’ >
 <SmartAssembly.MSBuild.Tasks.Build ProjectFile=SmartAssemblyPOC.saproj




  • Save the project
  • Load the project from solution explorer
  • I will quickly explain what the above lines mean. The UsingTask element maps the AssemblyName to the TaskName. Version is the version number of the SmartAssembly. If you are using version 5, change the version number to AfterBuild target is executed after the build is complete to obfuscate the smartassemblyPOC.exe in the release folder and then override it on the same folder. The $(OutDir) will be replaced by MSBuild with output folder of your assembly. The input and output in the MSBuild task overrides the input and output in the saproj
  • Build the project in Visual studio to make sure it compiles and the assembly gets obfuscated
  • It is not necessary to include the saproj file in the project as long as it is checked in. To include the saproj in the project:

  • Check in your changes
  • You need the SmartAssembly executable to be installed on your build machine otherwise Team Build won’t be able to find the SmartAssembly Tasks
  • After the Team build is done, view the log file
  • Notice the execution of the SmartAssembly task