Question3r
Question3r

Reputation: 3762

Can't pass in configuration section to IServiceCollection.Configure<T>() anymore

I have a console application and want to configure my Options by mapping the configuration from the appsettings.json file to a custom class. Packages I installed so far

Given this sample class

public class MyOptions
{
    public string Foo { get; set; }
}

and this configuration

{
    "myOptions": {
        "foo": "bar"
    }
}

I'm trying to map it with this code

public void ConfigureOptions(IServiceCollection serviceCollection)
{
    IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
    IConfiguration configuration = serviceProvider.GetService<IConfiguration>();
    IConfigurationSection myConfigurationSection = configuration.GetSection("MyOptions");
    serviceCollection.Configure<MyOptions>(myConfigurationSection);
}

Unfortunately this is invalid syntax. It is not possible to pass in the section to the Configure method anymore.

Argument type 'Microsoft.Extensions.Configuration.IConfigurationSection' is not assignable to parameter type 'System.Action<MyApp.MyOptions>'

This confuses me since there is a similar example in the docs

enter image description here

Does someone know if I'm missing something?

Upvotes: 12

Views: 4481

Answers (1)

Christian Held
Christian Held

Reputation: 2828

You are missing the Microsoft.Extensions.Options.ConfigurationExtensions package.

Upvotes: 29

Related Questions