@@ -44,6 +44,7 @@ IUnityContainer IUnityContainer.RegisterType(Type typeFrom, Type typeTo, string
4444 // Create registration and add to appropriate storage
4545 var container = manager is SingletonLifetimeManager ? _root : this ;
4646 var registration = new ContainerRegistration ( _validators , typeTo , manager , injectionMembers ) ;
47+ if ( manager is ContainerControlledLifetimeManager lifeteime ) lifeteime . Scope = container ;
4748
4849 // Add or replace existing
4950 var previous = container . Register ( registeredType , name , registration ) ;
@@ -125,6 +126,7 @@ IUnityContainer IUnityContainer.RegisterInstance(Type type, string name, object
125126 // Create registration and add to appropriate storage
126127 var container = manager is SingletonLifetimeManager ? _root : this ;
127128 var registration = new ContainerRegistration ( null , mappedToType , manager ) ;
129+ if ( manager is ContainerControlledLifetimeManager lifeteime ) lifeteime . Scope = container ;
128130
129131 // Add or replace existing
130132 var previous = container . Register ( typeFrom , name , registration ) ;
@@ -185,6 +187,7 @@ public IUnityContainer RegisterFactory(Type type, string name, Func<IUnityContai
185187#pragma warning restore CS0618
186188 var injectionMembers = new InjectionMember [ ] { injectionFactory } ;
187189 var registration = new ContainerRegistration ( _validators , type , manager , injectionMembers ) ;
190+ if ( manager is ContainerControlledLifetimeManager lifeteime ) lifeteime . Scope = container ;
188191
189192 // Add or replace existing
190193 var previous = container . Register ( type , name , registration ) ;
@@ -234,21 +237,24 @@ object IUnityContainer.Resolve(Type type, string name, params ResolverOverride[]
234237 if ( null == type ) throw new ArgumentNullException ( nameof ( type ) ) ;
235238
236239 var registration = ( InternalRegistration ) GetRegistration ( type , name ) ;
240+ var container = registration . Get ( typeof ( LifetimeManager ) ) is ContainerControlledLifetimeManager manager
241+ ? ( UnityContainer ) manager . Scope
242+ : this ;
243+
237244 var context = new BuilderContext
238245 {
239246 List = new PolicyList ( ) ,
240- Lifetime = LifetimeContainer ,
247+ Lifetime = container . LifetimeContainer ,
241248 Overrides = null != overrides && 0 == overrides . Length ? null : overrides ,
242249 Registration = registration ,
243250 RegistrationType = type ,
244251 Name = name ,
245252 ExecutePlan = ContextExecutePlan ,
246253 ResolvePlan = ContextResolvePlan ,
247- Type = registration is ContainerRegistration containerRegistration
248- ? containerRegistration . Type : type ,
254+ Type = registration is ContainerRegistration containerRegistration ? containerRegistration . Type : type ,
249255 } ;
250256
251- return ExecutePlan ( ref context ) ;
257+ return container . ExecutePlan ( ref context ) ;
252258 }
253259
254260 #endregion
@@ -266,10 +272,14 @@ object IUnityContainer.BuildUp(Type type, object existing, string name, params R
266272 if ( null != existing && null != TypeValidator ) TypeValidator ( type , existing . GetType ( ) ) ;
267273
268274 var registration = ( InternalRegistration ) GetRegistration ( type , name ) ;
275+ var container = registration . Get ( typeof ( LifetimeManager ) ) is ContainerControlledLifetimeManager manager
276+ ? ( UnityContainer ) manager . Scope
277+ : this ;
278+
269279 var context = new BuilderContext
270280 {
271281 List = new PolicyList ( ) ,
272- Lifetime = LifetimeContainer ,
282+ Lifetime = container . LifetimeContainer ,
273283 Existing = existing ,
274284 Overrides = null != overrides && 0 == overrides . Length ? null : overrides ,
275285 Registration = registration ,
0 commit comments