![](/img/trans.png)
[英].NET Framework Class Library with Microsoft.Extensions.DependencyInjection
[英]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. 在IServiceCollection
上,我试图调用带有一个部分的Configure
方法,如下所示:
IServiceCollection services = ...
IConfigurationSection section = ...
services.Configure<MySettings>(section);
如果我在 web 项目中做到这一点,效果会很好。 在 class 库项目中,不存在采用IConfigurationSection
的扩展方法。
在 web 项目中,这似乎来自 Microsoft.Extensions.DependencyInjection,但它必须在其他地方。 对于库项目,我包含了以下 NuGet 包。
<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>
您缺少Microsoft.Extensions.Options.ConfigurationExtensions package。
另请注意, OptionsConfigurationServiceCollectionExtensions.Configure
重载接受IConfiguration
接口,该接口是IConfigurationSection
的基础。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.