Gargoyle
Gargoyle

Reputation: 10324

Microsoft.Extensions.DependencyInjection in a class library

I'm making a C# class library, for .NET 6, that does a bunch of setup for web configuration and HTTP. On the IServiceCollection I'm trying to call the Configure method that takes a section, like so:

IServiceCollection services = ...
IConfigurationSection section = ...


services.Configure<MySettings>(section);

If I do that right in a web project it works great. In the class library project, the extension method that takes an IConfigurationSection doesn't exist.

In the web project it seems like this comes from Microsoft.Extensions.DependencyInjection, but it must be somewhere else. For the library project, I included the following NuGet packages.

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.13" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.15.0" />
  </ItemGroup>

Upvotes: 1

Views: 2423

Answers (1)

Guru Stron
Guru Stron

Reputation: 142123

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

Also note that OptionsConfigurationServiceCollectionExtensions.Configure overloads accept IConfiguration interface, which is base for IConfigurationSection.

Upvotes: 11

Related Questions