Home > General > Multiple threads and refreshing Spring.NET’s context

Multiple threads and refreshing Spring.NET’s context

One of our legacy applications uses Spring.NET as a service locator. In most cases, I generally agree that using a service locator is an anti-pattern. Unfortunately, this code is littered throughout the project:

var context = ContextRegistry.GetContext() as IConfigurableApplicationContext;
IService service = context.GetObject("name-of-object") as IService;

Spring.NET exposes a method that refreshes all the object definitions.

var context = ContextRegistry.GetContext() as IConfigurableApplicationContext;
context.Refresh();

Unfortunately, if another thread is attempting to resolve an object from the container while the context is being refreshed, a NullReferenceException is thrown.

System.NullReferenceException: Object reference not set to an instance of an object.
   at Spring.Context.Support.AbstractApplicationContext.GetObject(String name) in c:\_prj\spring-net\trunk\src\Spring\Spring.Core\Context\Support\AbstractApplicationContext.cs:line 1538
   at ConsoleDemo.Program.<Main>b__1() in C:\projects\ConsoleDemo\ConsoleDemo\Program.cs:line 30

A simple solution is to utilize the ReaderWriterLockSlim class to allow multiple threads to resolve objects while allowing us to block all the threads when we need to refresh the object definitions.

public static class ServiceLocator
{
    private static readonly ReaderWriterLockSlim locker;

    static ServiceLocator()
    {
        locker = new ReaderWriterLockSlim();
    }

    public static T GetObject<T>(string name) 
        where T : class
    {
        locker.EnterReadLock();
        try
        {
            var context = ContextRegistry.GetContext() as IConfigurableApplicationContext;
            return context.GetObject(name) as T;
        }
        finally
        {
            locker.ExitReadLock();
        }
    }

    public static void RefreshContext()
    {
        locker.EnterWriteLock();
        try
        {
            var context = ContextRegistry.GetContext() as IConfigurableApplicationContext;
            context.Refresh();
        }
        finally
        {
            locker.ExitWriteLock();
        }
    }
}
  1. September 10, 2014 at 4:09 pm

    Launching PR, main website and email promotion, and paid campaigns in as coordinated an effort as possible
    will help drive a burst of interest and hopefully downloads.

    Unveiling a business or website is not an easy task; it carries lots
    of pressure in the bearer mind. The features of the greatest SEO agency will be characterised in this form of
    method which they provide the top work in the internet search engine optimizer.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: