I am trying to use an option pattern with Autofac and every attempt has just resulted in errors.
What I've tried:
- Using the ConfigurationBuilder to retrieve an IConfiguration/IConfigurationRoot.
- Register an instance of
TestSectionOptionsusing the IConfiguration/IConfigurationRoot that was created before:builder.Register(c => config.GetSection("TestSection").Get<TestSectionOptions>()); - Trying to inject it via constructor injection:
private readonly TestSectionOptions _options;
public DemoClass(IOptions<TestSectionOptions> options)
{
_options = options.Value;
}
I'm getting following error:
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'DemoApp.DemoClass' can be invoked with the available services and parameters: Cannot resolve parameter 'Microsoft.Extensions.Options.IOptions
1[DemoApp.TestSectionOptions] options' of constructor 'Void .ctor(Microsoft.Extensions.Options.IOptions1
Of course I tried other types of registration, but none of them worked. I also know that I can simply bind the configuration file to a class, which I then register and inject without the IOptions<> part. But that would no longer correspond exactly to the option pattern, would it? Even if it doesn't make a big difference, I'd still like to know why it doesn't work and how I could get it to work.