question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to resolve IOptions<T> from child container

See original GitHub issue

I 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 = OptionsManager1 (ReflectionActivator), Services = [Microsoft.Extensions.Options.IOptions1[[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.IOptionsFactory1[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.OptionsManager1[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.IOptionsFactory1[Dotnettency.Tests.MyOptions])’.

Stack Trace:

at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func1 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, IEnumerable1 parameters) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable1 parameters, Object& instance) at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable1 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(IEnumerable1 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.IOptions1[[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.IOptionsFactory1[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.OptionsManager1[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.IOptionsFactory1[Dotnettency.Tests.MyOptions])’.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dazinatorcommented, Aug 27, 2018

@tillig PR #33 submitted.

0reactions
dazinatorcommented, Aug 27, 2018

Bingo that was it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Unable to resolve service for type IOptions ...
It turns out that all I was missing was: services.AddOptions();. Once I added that, the IOptions binding was registered correctly!
Read more >
How to use the IOptions pattern for configuration in ASP. ...
In this post I demonstrate the power of the IOptions<> pattern for configuration in ASP.NET Core, and describe a few of the problems...
Read more >
Dependency injection guidelines - .NET
Services resolved from the container should never be disposed by the developer. If a type or factory is registered as a singleton, the...
Read more >
Validated Strongly Typed IOptions - Kaylumah
An IOptions<> is used to make a configuration available as a strongly ... triggers the moment we resolve an IOptions from the DI...
Read more >
ASP.NET Core — Autofac 7.0.0 documentation
Controllers aren't resolved from the container; just controller constructor parameters. That means controller lifecycles, property injection, and other things ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found