Is the Simple Injector thread-safe?

Mar 24, 2012 at 11:48 PM
Edited Jan 14, 2015 at 9:20 AM
Yes, it is. Simple Injector is carefully designed to be both fast and thread-safe. The container is safe to use in any multi-threaded .NET and Silverlight application.

After the registration phase, instances can be safely resolved simultaneously over any number of threads. During the happy path, the container will do this in a lock-free manner, which maximizes the performance (you can read here how this is done).

Configuration of the container is expected to be done by in the start-up path of the application (such as Global.asax in the context of a web application), and on a single thread. The registration methods are therefore not thread-safe, and you should not use multiple threads to configure the container. After the registration phase the container is locked-down, and the Container.Register overloads cannot be called anymore.

The lock-free design of the container however, does come with special attention for developers who want to extend the container. Especially when working with the ResolveUnregisteredType, ExpressionBuilding and ExpressionBuilt events, that are exposed by the Container class. Although those events will usually only be called once per service type, developers must realize that those events can be raised simultaneously for the same service type by multiple threads. The code that gets called by those events must therefore be thread-safe. This is usually a no-brainer, but must be carefully reviewed, especially when that code uses any shared state.
Marked as answer by dot_NET_Junkie on 11/3/2013 at 11:50 PM