Unity – per thread lifetime manager

While working with multi-threaded applications build over a DI framework it would be extremely useful to have objects that are specific to the context of a thread. My most favorite IoC framework, Unity, provides awesome lifetime managers that can be used to handle situations like these.

Scenarios

The most common scenario where I came across this is to group application logs by a specific thread. Each thread does a specific job and I wanted to prefix specific attribute to the log. A smart lazy person would probably just start logging the thread ID. I know that is quite simple for you to understand the logs. But when a somewhat non-technical person (project managers) wish to see the logs, you might want to make the logs more meaningful.

I once worked on this application that performed a lot of calculations on separate threads. A complex calculation was forked into multiple threads. I wanted to store the output for reuse within a thread. (Lets skip the nitty-gritties of
the app). In that situation a ‘PerThreadStore’ came in extremely handy.

ThreadStatic attribute

There is a very simple way to create thread specific properties. Just use the [ThreadStatic] attribute. Quite common among the multi-threaded developers. But using this as an extension of your IoC framework makes the application quite cool to develop.

PerThreadLifetimeManager with Unity

    /// <summary>
    /// Extends the LifetimeManager to define a class that provides a lifetime per thread
    /// </summary>
    public class PerThreadLifetimeManager : LifetimeManager
    {
        /// <summary>
        /// Overridden method that returns the value of the stored object
        /// </summary>
        /// <returns>The value of the object</returns>
        public override object GetValue()
        {
            return PerThreadStore.Item;
        }

        /// <summary>
        /// Overridden method that removes the value for the stored object
        /// </summary>
        public override void RemoveValue()
        {
            PerThreadStore.Item = null;
        }

        /// <summary>
        /// Overridden method that adds a value to the stored object
        /// </summary>
        /// <param name="newValue">The value to be stored</param>
        public override void SetValue(object newValue)
        {
            PerThreadStore.Item = newValue;
        }
    }

    /// <summary>
    /// A store that stores a static object that has a single value per thread
    /// </summary>
    public class PerThreadStore
    {
        [ThreadStatic]
        private static object _Item;

        /// <summary>
        /// Gets or sets any value to be stored per thread
        /// </summary>
        public static object Item
        {
            get
            {
                return _Item;
            }
            set
            {
                _Item = value;
            }
        }
    }

Peace

2 Replies to “Unity – per thread lifetime manager”

  1. This would follow the thread lifetime. So the objects will be disposed with the thread. Objects created within this lifetime manager would be treated by GC just like other objects.

    In my experience, its best to let GC do its regular cleanups rather than trying to force a collector check.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.