Is "UnityContainer.Resolve" thread safe?

May 10, 2008 at 4:02 PM
Hi,

Is UnityContainer.Resolve method thread safe?  Can multiple threads call Resolve on the same container instance at the same time (for the same type maybe) without things going horribly wrong?  I couldn't see anything mentioned regarding thread safety in the documentation.

Thanks!

--
Ranju. V
http://blogorama.nerdworks.in/
--
May 10, 2008 at 6:39 PM
Yes, Resolve, ResolveAll, and BuildUp are all thread safe. Looks like we missed it in the docs.

None of the other methods on the container are thread safe, so if you're going to be configuring containers, you'll need to do locking. Also, if you've got one thread configuring and another thread resolving, there will be thread issues.

May 11, 2008 at 6:28 AM
Thanks ctavares, that helps!

--
Ranju. V
http://blogorama.nerdworks.in/
--
Feb 4, 2009 at 8:45 PM

I’ve found that unity container passes a reference to lifetimeContainer field (of type LifetimeContainer) to the builder. LifetimeContainer is not thread safe and not immutable, and I didn’t found any synchronization logic in the methods accessing it.

For example LifetimeStrategy queries LifetimeManagerFactory, which will write to lifetime container when lifetimeType implements IDisposable interface (consider public ILifetimePolicy CreateLifetimePolicy() method).

Also, if you create child container with CreateChildContainer() method, they will add themself to its parent container’s livetime container during creation and remove when disposing. So, disposing child containers from separate threads may lead to errors, even each child container accessed only by one thread at time.

When using custom strategies and policies, you should always consider that it may be accessed from different threads. For example SingletonLifetimePolicy written with such consideration but some custom components mind not and mind be sources of errors.

I think that the documentation really lacks good explanation of unity container usage rules in multithread environments and considerations must be taken when developing custom policies and strategies. This is an actual problem, because unity containers frequently used in multithread applications, such as WCF services and asp.net sites (as an instance providers and web page initializes accordingly).

And I'm really interested is UnityContainer.Resolve method *always* thread safe?
P.S.: sorry for my English.

Feb 5, 2009 at 7:28 AM
The specification of Resolve is that it is thread safe. If you find a situation where it isn't so, then it's a bug that we need to fix.

As such, your question boils down to "are there guaranteed no bugs in Resolve?" The answer of course is no, since there's no way to guarantee that for anything. We believe it to be properly thread safe, but if you find any issues please let us know.


Nov 29, 2009 at 8:20 PM

Can you elaborate on this:

   "if you've got one thread configuring and another thread resolving, there will be thread issues"

In my situation, I got a main thread that is configuring a child container, resolving a command object and dispatching to a threadpool. So far I see the following:

  1. I have to lock the parent/root container when I dispose the child container from the job thread.
  2. After about 10K jobs, memory is constant but the processor is pegged and the app slows way down.
  3. If I move the child container config and resolve to the thread performing the work, the app is fine.

My question is two-fold:

  1. What could be causing this?
  2. Seems similar to a web app in that the container is configured on one thread and then used on different threads. Is this an issue?
Jan 6, 2012 at 2:48 AM

Resolve is thread-safe, but some other methods that you'd think would be harmless are not. For example, I found that the Registrations property in Unity 2.0 enumerates the collection of registrations in such a way that if another thread is adding to the collection at the same time, there's trouble.

I coded a wrapper class to access the container. You can grab the code on my blog here: A Thread-Safe, Global Unity Container.

Sep 12, 2012 at 11:53 AM
LSpencer777 wrote:

Resolve is thread-safe, but some other methods that you'd think would be harmless are not. For example, I found that the Registrations property in Unity 2.0 enumerates the collection of registrations in such a way that if another thread is adding to the collection at the same time, there's trouble.

I coded a wrapper class to access the container. You can grab the code on my blog here: A Thread-Safe, Global Unity Container.

Link is bad