diff --git a/src/Autofac/ContainerBuilder.cs b/src/Autofac/ContainerBuilder.cs index 7073caa56..672fc1e37 100644 --- a/src/Autofac/ContainerBuilder.cs +++ b/src/Autofac/ContainerBuilder.cs @@ -172,6 +172,7 @@ private static void StartStartableComponents(IComponentContext componentContext) /// /// An existing container to make the registrations in. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "You can't update any arbitrary context, only containers.")] + [Obsolete("Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.")] public void Update(IContainer container) { Update(container, ContainerBuildOptions.None); @@ -188,6 +189,7 @@ public void Update(IContainer container) /// An existing container to make the registrations in. /// Options that influence the way the container is updated. [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "You can't update any arbitrary context, only containers.")] + [Obsolete("Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.")] public void Update(IContainer container, ContainerBuildOptions options) { // Issue #462: The ContainerBuildOptions parameter is added here as an overload @@ -208,7 +210,23 @@ public void Update(IContainer container, ContainerBuildOptions options) /// - this prevents ownership issues for provided instances. /// /// An existing registry to make the registrations in. + [Obsolete("Containers should generally be considered immutable. Register all of your dependencies before building/resolving. If you need to change the contents of a container, you technically should rebuild the container. This method may be removed in a future major release.")] public void Update(IComponentRegistry componentRegistry) + { + this.UpdateRegistry(componentRegistry); + } + + /// + /// Configure an existing registry with the component registrations + /// that have been made. Primarily useful in dynamically adding registrations + /// to a child lifetime scope. + /// + /// + /// Update can only be called once per + /// - this prevents ownership issues for provided instances. + /// + /// An existing registry to make the registrations in. + internal void UpdateRegistry(IComponentRegistry componentRegistry) { if (componentRegistry == null) throw new ArgumentNullException(nameof(componentRegistry)); Build(componentRegistry, true); diff --git a/src/Autofac/Core/Lifetime/LifetimeScope.cs b/src/Autofac/Core/Lifetime/LifetimeScope.cs index 5bc8750d7..8d9d936fe 100644 --- a/src/Autofac/Core/Lifetime/LifetimeScope.cs +++ b/src/Autofac/Core/Lifetime/LifetimeScope.cs @@ -217,7 +217,7 @@ private ScopeRestrictedRegistry CreateScopeRestrictedRegistry(object tag, Action configurationAction(builder); var locals = new ScopeRestrictedRegistry(tag, builder.Properties); - builder.Update(locals); + builder.UpdateRegistry(locals); return locals; } diff --git a/src/Autofac/Module.cs b/src/Autofac/Module.cs index 96db01dff..ed6c12736 100644 --- a/src/Autofac/Module.cs +++ b/src/Autofac/Module.cs @@ -79,7 +79,7 @@ public void Configure(IComponentRegistry componentRegistry) var moduleBuilder = new ContainerBuilder(componentRegistry.Properties); Load(moduleBuilder); - moduleBuilder.Update(componentRegistry); + moduleBuilder.UpdateRegistry(componentRegistry); AttachToRegistrations(componentRegistry); AttachToSources(componentRegistry); } diff --git a/test/Autofac.Test/ContainerBuilderTests.cs b/test/Autofac.Test/ContainerBuilderTests.cs index b8d8d99fa..4422fad7c 100644 --- a/test/Autofac.Test/ContainerBuilderTests.cs +++ b/test/Autofac.Test/ContainerBuilderTests.cs @@ -361,7 +361,9 @@ public void WhenUpdating_DefaultModulesAreExcluded() { var builder = new ContainerBuilder(); var container = new Container(); +#pragma warning disable CS0618 builder.Update(container); +#pragma warning restore CS0618 Assert.False(container.IsRegistered>()); } @@ -445,7 +447,9 @@ public void WhenTheContainerIsUpdated_ExistingStartableComponentsAreNotReStarted var builder2 = new ContainerBuilder(); builder2.RegisterInstance(startable2).As(); +#pragma warning disable CS0618 builder2.Update(container); +#pragma warning restore CS0618 Assert.Equal(1, startable1.StartCount); Assert.Equal(1, startable2.StartCount); @@ -461,7 +465,9 @@ public void WhenTheContainerIsUpdated_NewStartableComponentsAreStarted() var builder = new ContainerBuilder(); builder.RegisterInstance(startable).As(); +#pragma warning disable CS0618 builder.Update(container); +#pragma warning restore CS0618 Assert.Equal(1, startable.StartCount); } diff --git a/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs b/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs index 88a0d95f9..844a378e8 100644 --- a/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs +++ b/test/Autofac.Test/Core/Lifetime/LifetimeScopeTests.cs @@ -418,7 +418,7 @@ internal void UpdateRegistry(object instance) { var builder = new ContainerBuilder(); builder.RegisterInstance(instance); - builder.Update(_registerContext.ComponentRegistry); + builder.UpdateRegistry(_registerContext.ComponentRegistry); } } diff --git a/test/Autofac.Test/Core/Registration/CopyOnWriteRegistryTests.cs b/test/Autofac.Test/Core/Registration/CopyOnWriteRegistryTests.cs index a4b28fce9..9982a347e 100644 --- a/test/Autofac.Test/Core/Registration/CopyOnWriteRegistryTests.cs +++ b/test/Autofac.Test/Core/Registration/CopyOnWriteRegistryTests.cs @@ -44,7 +44,7 @@ public void RegistrationsMadeByUpdatingAChildScopeDoNotAppearInTheParentScope() var childScope = container.BeginLifetimeScope(); var updater = new ContainerBuilder(); updater.RegisterType(); - updater.Update(childScope.ComponentRegistry); + updater.UpdateRegistry(childScope.ComponentRegistry); Assert.True(childScope.IsRegistered()); Assert.False(container.IsRegistered()); } diff --git a/test/Autofac.Test/RegistrationExtensionsTests.cs b/test/Autofac.Test/RegistrationExtensionsTests.cs index b24abd5bd..ca1ea1aa7 100644 --- a/test/Autofac.Test/RegistrationExtensionsTests.cs +++ b/test/Autofac.Test/RegistrationExtensionsTests.cs @@ -228,7 +228,9 @@ public void AutoActivate_ContainerUpdateAutoActivatesNewComponents() var container = new ContainerBuilder().Build(); var builder = new ContainerBuilder(); builder.RegisterType().AutoActivate().OnActivated(e => instanceCount++); +#pragma warning disable CS0618 builder.Update(container); +#pragma warning restore CS0618 Assert.Equal(1, instanceCount); } @@ -245,7 +247,9 @@ public void AutoActivate_ContainerUpdateDoesNotAutoActivateExistingComponents() int secondCount = 0; var builder2 = new ContainerBuilder(); builder2.RegisterType().AutoActivate().OnActivated(e => secondCount++); +#pragma warning disable CS0618 builder2.Update(container); +#pragma warning restore CS0618 Assert.Equal(1, firstCount); Assert.Equal(1, secondCount); }