1
Vote

Instantiating parent singleton in a child container causes dangerous behavior

description

<p>Steps to reproduce:</p> <p>&nbsp;</p> <p>1. Type B depends on type A.</p> <p>2. Type A is registered as singleton in both parent and child containers.</p> <p>3. Type B is registered as singleton only in parent container, but not in child container.</p> <p>&nbsp;</p> <p>If first instantiation of B is done via parent, everything is as expected. However, if first instantiation of B is done via child, it will be built up in child&#39;s context, and then installed as a singleton on the parent level. If the child is later disposed, B&#39;s instance of A will also be disposed, creating a hanging dependency.</p> <p>&nbsp;</p> <p>This behavior can cause bugs in two ways:</p> <p>&nbsp;</p> <p>* It may be very difficult to predict whether first instantiation of B will occur via parent or child container, as it may be implicit via [Dependency] attribute.</p> <p>&nbsp;</p> <p>* After the child container is disposed, the instance of B received from the parent container will be unusable without a way to repair it.</p>

file attachments

comments