diff --git a/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs b/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs index 7688ee77f..e17c0f57d 100644 --- a/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs +++ b/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs @@ -48,13 +48,13 @@ internal class ServiceRegistrationInfo /// List of service implementations coming from sources. Sources have priority over preserve-default implementations. /// Implementations from sources are enumerated in preserve-default order, so the most default implementation comes first. /// - private readonly List _sourceImplementations = new List(); + private List _sourceImplementations = null; /// /// List of explicit service implementations specified with the PreserveExistingDefaults option. /// Enumerated in preserve-defaults order, so the most default implementation comes first. /// - private readonly List _preserveDefaultImplementations = new List(); + private List _preserveDefaultImplementations = null; [SuppressMessage("Microsoft.ApiDesignGuidelines", "CA2213", Justification = "The creator of the compponent registration is responsible for disposal.")] private IComponentRegistration _defaultImplementation; @@ -88,10 +88,18 @@ public IEnumerable Implementations get { RequiresInitialization(); - return Enumerable - .Reverse(_defaultImplementations) - .Concat(_sourceImplementations) - .Concat(_preserveDefaultImplementations); + var resultingCollection = Enumerable.Reverse(_defaultImplementations); + if (_sourceImplementations != null) + { + resultingCollection = resultingCollection.Concat(_sourceImplementations); + } + + if (_preserveDefaultImplementations != null) + { + resultingCollection = resultingCollection.Concat(_preserveDefaultImplementations); + } + + return resultingCollection; } } @@ -114,18 +122,32 @@ public bool IsRegistered } private bool Any => - _defaultImplementations.Any() || - _sourceImplementations.Any() || - _preserveDefaultImplementations.Any(); + _defaultImplementations.Count > 0 || + _sourceImplementations != null || + _preserveDefaultImplementations != null; public void AddImplementation(IComponentRegistration registration, bool preserveDefaults, bool originatedFromSource) { if (preserveDefaults) { if (originatedFromSource) + { + if (_sourceImplementations == null) + { + _sourceImplementations = new List(); + } + _sourceImplementations.Add(registration); + } else + { + if (_preserveDefaultImplementations == null) + { + _preserveDefaultImplementations = new List(); + } + _preserveDefaultImplementations.Add(registration); + } } else { @@ -144,8 +166,8 @@ public bool TryGetRegistration(out IComponentRegistration registration) registration = _defaultImplementation ?? (_defaultImplementation = _defaultImplementations.LastOrDefault() ?? - _sourceImplementations.FirstOrDefault() ?? - _preserveDefaultImplementations.FirstOrDefault()); + _sourceImplementations?.First() ?? + _preserveDefaultImplementations?.First()); return registration != null; } @@ -214,7 +236,7 @@ public bool ShouldRecalculateAdaptersOn(IComponentRegistration registration) // - Have already been initialized // - Were created via a registration source (because we might be adding an equivalent explicit registration such as Func) // - Don't contain any registrations (because a registration source was added when no adaptee was present) - return IsInitialized && (_sourceImplementations.Any() || !Any); + return IsInitialized && (_sourceImplementations != null || !Any); } } } \ No newline at end of file