Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested scope doesn't see new registrations in parent scope #608

Closed
ilya-g opened this issue Dec 23, 2014 · 5 comments
Closed

Nested scope doesn't see new registrations in parent scope #608

ilya-g opened this issue Dec 23, 2014 · 5 comments

Comments

@ilya-g
Copy link

ilya-g commented Dec 23, 2014

Changes to the parent component registry don't propagate well to the nested registry when the service in the nested registry is already initialized.

Steps to reproduce

  1. Create parent and nested scopes. Make nested scope to create it's own registry.

        var container = new ContainerBuilder().Build();
        var nested = container.BeginLifetimeScope(_ => { });
  2. Make nested scope to initialize service registration info for some service:

            nested.Resolve<IEnumerable<string>>(); 
  3. Update parent scope and try to resolve updated service implementation from nested scope

            var builder = new ContainerBuilder();
            builder.RegisterInstance("s1");
            builder.Update(container);
    
            Assert.That(nested.Resolve<string>(), Is.EqualTo("s1"), "Nested scope should get update from the parent scope");

Expected output
Updated implementation gets resolved from nested scope.

Aсtual output
Autofac.Core.Registration.ComponentNotRegisteredException : The requested service 'System.String' has not been registered.

ilya-g added a commit to ilya-g/Autofac that referenced this issue Dec 23, 2014
@tillig
Copy link
Member

tillig commented Jul 27, 2015

By and large, scopes, once created are immutable. The Update method is kind of a hack to get things into a single scope, but generally it's truly just a hack. When you create a child scope, the parent's registry gets copied over into the child. Updating the parent doesn't update the copy with the child.

I'll mark this as an enhancement. There are some other internal things we want to fix and this would go along with those, possibly even being fixed as a byproduct of fixing the other issues.

@alexmg
Copy link
Member

alexmg commented Jul 28, 2015

This isn't something that I have ever run into before. Do you have an example of a situation where this has been required?

@tillig
Copy link
Member

tillig commented Oct 25, 2016

I think we're going to close this as "won't fix." Scopes, generally speaking, are immutable once you create them and updating a container out from under a scope will actually cause more problems than it solves - for example, you may have already resolved something from the child scope that is incorrect now based on changes made in the parent scope after the resolution takes place.

Instead, I'd recommend looking at ways to get around actually modifying the parent scope and hoping the child scope sees new parent registrations post-creation. In general, I'd say best practice is to really try to treat containers as immutable.

@tillig
Copy link
Member

tillig commented Oct 25, 2016

It appears [Nancy bootstrapping] uses Update as a way to add registrations to the container post-facto.

I guess I'll open this back up to see if we need to do something here, though I still feel this is a "won't fix." I'd just as soon remove the "Update" method to avoid the challenges/inconsistencies it causes in various behaviors like AutoActivate and IStartable.

@tillig
Copy link
Member

tillig commented Apr 18, 2017

Re-closing as won't fix. Nancy has an issue to handle removal of their use of Update and when we treat a container as immutable this won't be an issue.

@tillig tillig closed this as completed Apr 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants