|
Autofac2Concurrency
Concurrency in Autofac 2.x
Autofac2 IntroductionAutofac is designed from the ground up to be used in multi-threaded applications. This page summarises the approach to thread safety for both users and the Autofac maintainers. Thread-Safe TypesThe following types are safe for concurrent access by multiple threads:
These types cover practically all of the runtime/resolution scenarios. The following types are designed for single-threaded access at configuration time:
So, a correct Autofac application will use a ContainerBuilder on a single thread to create the container at startup. Subsequent use of the container can occur on any thread. ImportantThe temporary IComponentContext parameter c passed to registration delegates is not thread safe. The following code is broken: builder.Register(c => new Foo(c)) The correct version of the above is: builder.Register(c => new Foo(c.Resolve<IComponentContext>())) Deadlock AvoidanceAutofac is designed in such a way that deadlocks won't occur in normal use. This section is a guide for maintainers or extension writers. Lock Acquisition OrderLocks may be acquired in the following order:
Rules of Thumb
| |