Home > Net >  Microsoft.Extensions.DependencyInjection in a class library
Microsoft.Extensions.DependencyInjection in a class library

Time:01-07

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>

CodePudding user response:

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

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

  •  Tags:  
  • Related