Search code examples
.netsingletonioc-containerthreadcontext

IoC and thread singletons


Several different IoC containers have support for Thread Singletons.

How do the frameworks know when a thread goes out of scope?

For instance: If I use the Thread Pool to do something, how can the container know when to create and dispose a service? The thread can be used for various of things and each task wants a new service, doesn't it?


Solution

  • In Autofac you'd do this manually by creating a scope around the thread entry point (can give example if required.) Maybe not so 'magic' as implicit thread scoping, but along the lines of what you're observing here, it is much easier to understand/test/debug.

    IMHO :)

    Nick