-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
Configuration on ServiceCollection being ignored when using Populate on ContainerBuilder in a nested lifetime scope #659
Comments
Try passing in the https://github.com/autofac/Autofac/blob/master/samples/AutofacWebApiSample/Startup.cs In the sample Startup.cs file above line 29 would be changed to the following.
|
I am not sure that will fix the issue. So I think the issue, resides with how Options Are transferred from the Service Collection to Autofac Container using the Autofac Registration Populate method. I am registering an Autofac module within a lifetimescope (https://github.com/OrchardCMS/Brochard/blob/78dcedf7cf2019ebeceb16baaf2d3122701568ad/src/OrchardVNext/Environment/ShellBuilders/ShellContainerFactory.cs#L72) - the module it registers is called MvcModule All dependencies get transferred to the Autofac Registration in the Populate Method including a IConfigurateOption .... however, when this is transferred across the options are lost.... i.e. the Action is not getting called, so the state is lost. The action in my above instance...
What do you think? |
I have a small test project here https://github.com/Jetski5822/AutofacSample/blob/master/Startup.cs which shows the issue. D:\AutofacSample>dnx . web Registering Module |
Hey guys, any idea on how to fix this? |
Looking at the repro posted by @Jetski5822 it appears the problem is specifically encountered when registering the options in a nested lifetime scope. The repro shows that it works correctly when registered directly into the root container and I've independently verified that. |
I figured it out, and I don't think there's anything we can do to fix this. The problem is in Microsoft.Extensions.OptionsModel, in When you use the That call is done for you indirectly by the call to When services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>))); The important part there is that it's adding a singleton registration. Singletons get resolved - always - from the root container, not from a child lifetime scope. When you (or one of the framework components, like the razor view engine) ask for an
When you call the The singleton registration means that the It works when you register everything in the root container rather than a nested child lifetime scope as evidenced by the @Jetski5822 demo project and our own unit tests. Basically, since the configuration actions aren't registered in the root container... you're not going to get what you expect. There are two ways to work with this:
In any case, it's not something Autofac core can actually do anything about. The resolution of singleton dependencies from the root lifetime scope is correct and desired behavior. |
@Jetski5822 , I could manage the same thing you are after as follows: Autofac Module
Test with dependencies: "Autofac" :"4.1.0" Please not that, I don't use services.configure in my Startup.cs and I haven't tested yet but, you must still be able to add additional options with that since OptionsManager requires a IEnumerable<IConfigureOptions> |
Reopening this issue so we have something to attach to the PR. |
Pulled #14. This will go out shortly as 4.2.0. Thanks for all the help! |
Using 4.0.0-alpha4-73
I have noticed that when calling Populate with a service container that contains some configuration, the configuration itself is being ignored in all future requests.
Any ideas on how to fix? or am I calling Configure in the wrong way?
The text was updated successfully, but these errors were encountered: