'IServiceCollection' does not contain a definition for 'Configuration' even though IntelliSense suggests otherwise
Asked Answered
D

5

15

I am having a strange problem here. I created a Worker project in order to create a Windows Service in .NET 6 following this documentation. I want to read the settings from appsettings.json so I added the following code:

IHost host = Host.CreateDefaultBuilder(args)
    .UseWindowsService(options =>
    {
        options.ServiceName = "My Service";
    })
    .ConfigureServices(services =>
    {
        var settings = new ScriptOptions(); // ScriptOptions is just a POCO class
        services.Configuration.Bind(settings);

        services.AddHostedService<WindowsBackgroundService>();
    })
    .Build();

As you can see, IntelliSense seems to recognize there is Configuration property in services (instance of IServiceCollection).

enter image description here

However, the code wouldn't compile with this error:

'IServiceCollection' does not contain a definition for 'Configuration' and no accessible extension method 'Configuration' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

What package am I missing? My project currently has:

    <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="6.0.0" />
Digitalin answered 15/11, 2021 at 8:25 Comment(0)
D
21

They fixed the bug. To access Configuration or other IHostBuilderContext members, use this overload:

    .ConfigureServices((ctx, services) =>
    {
        var settings = new ScriptOptions();
        ctx.Configuration.Bind(settings);

        services.AddHostedService<WindowsBackgroundService>();
    })

Original answer:

Seem to be an IntelliSense bug, I reported it here on VS Developer community here. If you encounter the same problem, please upvote it.

Looks like somehow IntelliSense was confused by the other overload. Indeed IServiceCollection does NOT have Configuration property, but HostBuilderContext does.

ConfigureServices has another overload that expose both parameters. This fixes the problem:

    // Add ctx parameter
    .ConfigureServices((ctx, services) =>
    {
        var settings = new ScriptOptions();
        ctx.Configuration.Bind(settings);

        services.AddHostedService<WindowsBackgroundService>();
    })
Digitalin answered 15/11, 2021 at 8:33 Comment(0)
R
19

Just install nuget package "Microsoft.Extensions.Options.ConfigurationExtensions" it will solve the problem, it has "Configure" extension method.

Roister answered 7/8, 2022 at 12:27 Comment(2)
You should not need that package. You only need to use the correct extension method. See my answer.Digitalin
I have installed "Microsoft.Extensions.Options.ConfigurationExtensions" package and it works fine, may you have another solution, but mine is the easiest.Roister
E
2

Add "Microsoft.Extensions.Options.ConfigurationExtensions" package.

in program class add to get secret.json:

        var configuration = new ConfigurationBuilder().AddUserSecrets<Program>().Build();

or get appsetting.json

var configuration = new ConfigurationBuilder().AddJsonFile(appsettings.json, optional: true, reloadOnChange: true).Build();

Get your secrets:

configuration["ServiceBus:CONNECTION_STRING"];
Ethbin answered 27/8, 2023 at 22:2 Comment(0)
T
0

The reason there is disagreement here between the 2 options is because there are actually 2 methods.

Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions has:

Configure<TOptions>(this IServiceCollection services, IConfiguration config)

Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions has:

Configure<TOptions>(this IServiceCollection services, Action<TOptions> configureOptions)

Note that the second parameter is different! Also they seem to come from different NuGet packages notably:

  • Microsoft.Extensions.Options.ConfigurationExtensions

and

  • Microsoft.Extensions.Options
Tunicle answered 25/1 at 22:9 Comment(0)
C
0

Just need to install NuGet package 'Microsoft.Extensions.Options' to your project.

Cooky answered 3/2 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.