gpt4 book ai didi

asp.net-core - 如何在 IHostingStartup 实现中访问配置?

转载 作者:行者123 更新时间:2023-12-05 06:11:40 33 4
gpt4 key购买 nike

不支持尝试从 IHostingStartup 实现和构造函数注入(inject)中访问 IConfiguration

正常的 Startup.cs 实现允许注入(inject) IConfiguration 使其易于从 ConfigureService() 中访问Configure() 方法。

获取配置访问权限的最佳实践方法是什么?

最佳答案

某些构建器方法(即 ConfigureLogging() ConfigureAppConfiguration() ConfigureServices() 允许 WebHostBuilderContext 重载传递到操作中。

WebHostBuilderContext提供对托管环境和配置的访问

    public class HostingStartupConfiguration : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder
.ConfigureLogging((context, builder) =>
{
// clear providers set from host application
if (context.HostingEnvironment.IsDevelopment())
{
...
}
})
.ConfigureAppConfiguration((context, builder) =>
{
if (context.HostingEnvironment.IsDevelopment())
Console.WriteLine("we are in dev mode");

...
})
.ConfigureServices((context, services) =>
{
...

// get assemblies based on configuration to load as Application Parts
var assemblies = GetControllerAssemblies(context.Configuration);

// register controllers application parts from external assemblies
foreach (var assembly in assemblies)
{
builder.AddApplicationPart(assembly);
}

...
});
}

关于asp.net-core - 如何在 IHostingStartup 实现中访问配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63894205/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com