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
).
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" />