Skip to content

Commit c441ace

Browse files
committed
Fixes singleton scope
1 parent 2dacc34 commit c441ace

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

src/Builder/Context/BuilderContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,18 @@ public object Resolve(Type type, string name, InternalRegistration registration)
152152
unsafe
153153
{
154154
var thisContext = this;
155+
var containerRegistration = registration as ContainerRegistration;
156+
var container = registration.Get(typeof(LifetimeManager)) is ContainerControlledLifetimeManager manager
157+
? ((UnityContainer)manager.Scope).LifetimeContainer
158+
: Lifetime;
159+
155160
var context = new BuilderContext
156161
{
157-
Lifetime = Lifetime,
162+
Lifetime = container,
158163
Registration = registration,
159164
RegistrationType = type,
160165
Name = name,
161-
Type = registration is ContainerRegistration containerRegistration ? containerRegistration.Type : type,
166+
Type = null != containerRegistration ? containerRegistration.Type : type,
162167
ExecutePlan = ExecutePlan,
163168
ResolvePlan = ResolvePlan,
164169
List = List,

src/UnityContainer.IUnityContainer.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/UnityContainer.Registration.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ private IRegistry<string, IPolicySet> Get(Type type)
316316

317317
#region Registration manipulation
318318

319+
// Register new and return overridden registration
319320
private IPolicySet AddOrUpdate(Type type, string name, InternalRegistration registration)
320321
{
321322
var collisions = 0;
@@ -422,11 +423,13 @@ private IPolicySet GetOrAdd(Type type, string name)
422423
}
423424
}
424425

426+
// Return generic registration or create from factory if not registered
425427
private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
426428
{
427429
var collisions = 0;
428430
int hashCode;
429431
int targetBucket;
432+
var factory = false;
430433

431434
if (null != _parent)
432435
{
@@ -440,10 +443,14 @@ private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
440443
continue;
441444
}
442445

443-
if (null != candidate.Value?[name]) break;
444-
445-
return _parent._getGenericRegistration(type, name, definition);
446+
if (null != candidate.Value?[name])
447+
{
448+
factory = true;
449+
break;
450+
}
446451
}
452+
453+
if (!factory) return _parent._getGenericRegistration(type, name, definition);
447454
}
448455

449456
hashCode = (type?.GetHashCode() ?? 0) & 0x7FFFFFFF;
@@ -489,8 +496,6 @@ private IPolicySet GetOrAddGeneric(Type type, string name, Type definition)
489496
_registrations.Buckets[targetBucket] = _registrations.Count++;
490497
return registration;
491498
}
492-
493-
494499
}
495500

496501
private IPolicySet Get(Type type, string name)

0 commit comments

Comments
 (0)