Unable to resolve IOptions<T> from child container
See original GitHub issueI am using:
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.3.0" />
I will submit a PR with a failing test case / repro. See repro: https://github.com/autofac/Autofac.Extensions.DependencyInjection/pull/33
The issue I am having, is occuring when trying to use the Options
pattern, provided by Microsoft.Extensions.Options
.
The issue is, if I register these services in the root container, everything works.
If I don’t register these services in the root container, but instead create a child lifetime scope, and register these services in the child lifetime scope only, then try to resolve an IOptions<T>
from that child scope, it errors.
Message:
Message: Autofac.Core.DependencyResolutionException : An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = OptionsManager
1 (ReflectionActivator), Services = [Microsoft.Extensions.Options.IOptions
1[[Dotnettency.Tests.MyOptions, Dotnettency.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope —> None of the constructors found with ‘Autofac.Core.Activators.Reflection.DefaultConstructorFinder’ on type ‘Microsoft.Extensions.Options.OptionsManager1[Dotnettency.Tests.MyOptions]' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.Extensions.Options.IOptionsFactory
1[Dotnettency.Tests.MyOptions] factory’ of constructor ‘Void .ctor(Microsoft.Extensions.Options.IOptionsFactory1[Dotnettency.Tests.MyOptions])'. (See inner exception for details.) ---- Autofac.Core.DependencyResolutionException : None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Microsoft.Extensions.Options.OptionsManager
1[Dotnettency.Tests.MyOptions]’ can be invoked with the available services and parameters: Cannot resolve parameter ‘Microsoft.Extensions.Options.IOptionsFactory1[Dotnettency.Tests.MyOptions] factory' of constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsFactory
1[Dotnettency.Tests.MyOptions])’.
Stack Trace:
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func
1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable
1 parameters)
at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable
1 parameters)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Dotnettency.Tests.AutofacOptionsTests.CanResolveTOptionsFromChildContainer() in F:\Repos\Dotnettency\src\Dotnettency.Tests\AutofacOptionsTests.cs:line 49
----- Inner Stack Trace -----
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 parameters)
Result Message:
Autofac.Core.DependencyResolutionException : An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = OptionsManager1 (ReflectionActivator), Services = [Microsoft.Extensions.Options.IOptions
1[[Dotnettency.Tests.MyOptions, Dotnettency.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope —> None of the constructors found with ‘Autofac.Core.Activators.Reflection.DefaultConstructorFinder’ on type ‘Microsoft.Extensions.Options.OptionsManager1[Dotnettency.Tests.MyOptions]' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.Extensions.Options.IOptionsFactory
1[Dotnettency.Tests.MyOptions] factory’ of constructor ‘Void .ctor(Microsoft.Extensions.Options.IOptionsFactory1[Dotnettency.Tests.MyOptions])'. (See inner exception for details.) ---- Autofac.Core.DependencyResolutionException : None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Microsoft.Extensions.Options.OptionsManager
1[Dotnettency.Tests.MyOptions]’ can be invoked with the available services and parameters:
Cannot resolve parameter ‘Microsoft.Extensions.Options.IOptionsFactory1[Dotnettency.Tests.MyOptions] factory' of constructor 'Void .ctor(Microsoft.Extensions.Options.IOptionsFactory
1[Dotnettency.Tests.MyOptions])’.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (1 by maintainers)
@tillig PR #33 submitted.
Bingo that was it.