[英]How to read configuration settings before initializing a Host in ASP .NET Core?
Before initializing the application Host I need to read some settings from the application's configuration to setup some other things.在初始化应用程序主机之前,我需要从应用程序的配置中读取一些设置来设置其他一些东西。
In ASP .NET Core 2.x to read settings before initializing the application Host I used to do the following:在 ASP .NET Core 2.x 中,在初始化应用程序主机之前读取设置我曾经执行以下操作:
public static void Main(string[] args)
{
//...
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddJsonFile("appsettings.json")
.Build();
//Do something useful with the configuration...
var host = WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.UseConfiguration(configuration)
.Build();
//...
}
In ASP .NET Core 3.x WebHost
has been deprecated in favor of .NET Generic Host .在 ASP .NET Core 3.x WebHost
已被弃用,取而代之的是 .NET Generic Host 。
.NET Generic Host has only .ConfigureHostConfiguration()
and .ConfigureAppConfiguration()
that do not take a built configuration as parameter, but instead accept only a delegate used to setup the configuration. .NET 通用主机只有 .ConfigureHostConfiguration .ConfigureHostConfiguration()
和 .ConfigureAppConfiguration .ConfigureAppConfiguration()
不将构建配置作为参数,而是仅接受用于设置配置的委托。
For HTTP workloads you can still use the method .UseConfiguration()
has it is exposed by IWebHostBuilder
and do essentially the same as before:对于 HTTP 工作负载,您仍然可以使用.UseConfiguration()
方法,它由IWebHostBuilder
公开,并且与以前基本相同:
public static void Main(string[] args)
{
//...
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddJsonFile("appsettings.json")
.Build();
//Do something useful with the configuration...
var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>()
.UseConfiguration(configuration);
})
.Build();
//...
}
But this only works for HTTP workloads and not for Worker Services.但这仅适用于 HTTP 工作负载,不适用于Worker 服务。
To get the configuration before setting up the Host I've come up with the following approach:为了在设置主机之前获取配置,我想出了以下方法:
public static void Main(string[] args)
{
//...
var configuration = ConfigureConfigurationBuilder(args)
.Build();
//Do something useful with the configuration...
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder => ConfigureConfigurationBuilder(args, builder))
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.Build();
//...
}
public static IConfigurationBuilder ConfigureConfigurationBuilder(string[] args, IConfigurationBuilder configurationBuilder = null)
{
configurationBuilder ??= new ConfigurationBuilder();
configurationBuilder
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddJsonFile("appsettings.json");
return configurationBuilder;
}
Essentially I've wrote a method that setups a ConfigurationBuilder
and returns it, so that I can reuse the same configuration.本质上,我编写了一个设置ConfigurationBuilder
并返回它的方法,以便我可以重用相同的配置。 This works in practice, but I build the same configuration twice.这在实践中有效,但我两次构建相同的配置。
Is there a simpler/more correct way (that works for HTTP workloads and non-HTTP workloads) to build and reuse the configuration before setting up the Host?在设置主机之前,是否有更简单/更正确的方法(适用于 HTTP 工作负载和非 HTTP 工作负载)来构建和重用配置?
You can clear the default sources added by CreateDefaultBuilder
then add a pre-built IConfiguration
with the AddConfiguration
extension method .您可以清除CreateDefaultBuilder
添加的默认源,然后使用AddConfiguration
扩展方法添加预构建的IConfiguration
。
public static void Main(string[] args)
{
//...
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.AddJsonFile("appsettings.json")
.Build();
//Do something useful with the configuration...
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder =>
{
builder.Sources.Clear();
builder.AddConfiguration(configuration);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.Build();
//...
}
The right question, do you realy need a configuration for your configuration?正确的问题,您真的需要为您的配置进行配置吗? you can use another file, and bind it directly with entity您可以使用另一个文件,并将其直接与实体绑定
configuration.GetSection("entity").Bind(entity);
If i didn't understand what you mean, all i know that the setup you will need it for dependency injection, or you can't use it, so instead of using this, you can use Extensions for your service collection to do what you want with your configuratin, and you get the setup in your container如果我不明白你的意思,我只知道你将需要它来进行依赖注入的设置,或者你不能使用它,所以你可以使用扩展来代替你的服务集合来做你想做的事想要你的配置,然后你在你的容器中得到设置
By default ConfigureAppConfiguration()
will load appsettings.json
according to the documentation Default builder settings .默认情况下, ConfigureAppConfiguration()
将根据文档Default builder settings加载appsettings.json
。
Alternatively, if you can't access your configuration or you are getting System.NullReferenceException: 'Object reference not set to an instance ofan object.'
或者,如果您无法访问您的配置或者您收到System.NullReferenceException: 'Object reference not set to an instance ofan object.'
error, it means the IHostBuilder
can't see the json file in it base path.错误,这意味着IHostBuilder
在其基本路径中看不到 json 文件。
Check or change the appsettings.json
file properties as seen in the image below:检查或更改appsettings.json
文件属性,如下图所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.