开发者

Resolve singleton object from several threads

开发者 https://www.devze.com 2023-02-16 11:30 出处:网络
I want to resolve the same object from different threads. But when I dispose (teardown) an object in one thread and resolve it in another thread, I get two different objects, although the object needs

I want to resolve the same object from different threads. But when I dispose (teardown) an object in one thread and resolve it in another thread, I get two different objects, although the object needs to be a singleton across the whole application. The LifetimeManager is a ContainerControlledLifetimeManager which sh开发者_如何学Pythonould behave like a singleton, but it doesn't when I have several threads.

What can I do about this issue?

More details: I think a part of the problem might be, that I register a new type mapping in one thread. Although I have synchronized the call the UnityContainer seems to hold a reference on the item built up before and provides it to the thread which calls resolve. The thread which registers the new type mapping gets the new item just fine.


ContainerControlledLifetimeManager holds the resolved instance just in private non static field. It is often called singleton but real meaning is singleton per lifetime manager instance. Once you register type again it will get new instance of ContainerControlledLifetimeManager and this instance of lifetime manager will hold its own instance of resolved type.

Simple: Two different type registrations = two different "singletons".

0

精彩评论

暂无评论...
验证码 换一张
取 消