使用应用程序设置来驱动特定于环境的设置,例如UseUrls [英] Use appsettings to drive environment specific settings such as UseUrls
问题描述
当我使用VS Code在本地进行开发时,我将使用端口3000,因为我是一个赶时髦的人.非时髦人士希望在服务器的端口8080上使用它.太酷了,我们明白了. Microsoft文档为我提供了以下示例:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true)
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.Configure(app =>
{
app.Run(async (context) => await context.Response.WriteAsync("Hi!"));
})
.Build();
host.Run();
}
我不想使用hosting.json
.我为什么要那个?我完全有这种appsettings.{environment}.json
文件.亲爱的,我要把那个坏男孩贴进去
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddCommandLine(args)
.Build();
什么是编译器错误? 那么,我该如何解决这个问题? env
在当前上下文中不存在.它仅存在于Startup.cs
文件中,该文件在启动时不会被调用,而在带有魔术的启动文件Program.cs
中被称为.
appsettings.json
中,然后在通过Program.cs
中的WebHostBuilder
构建特定于环境的Web主机时使用它?
这是可能的.通过在此处中给出的答案进行扩展,通过在Program.cs中创建WebHostBuilder和ConfigurationBuilder,可以访问主机环境,然后在特定于环境的appsettings文件中配置主机URL和端口.
假设一个appsettings.json和一个apppsettings.Development.json文件分别具有以下内容:
"hostUrl": "http://*:<port number here>"
用以下内容修改主线:
public static void Main(string[] args)
{
var host = new WebHostBuilder();
var env = host.GetSetting("environment");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env}.json", optional: true)
.AddEnvironmentVariables();
var configuration = builder.Build();
host.UseKestrel()
.UseUrls(configuration["hostUrl"])
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build()
.Run();
}
使用此代码,Startup.cs仍将需要声明其自己的ConfigurationBuilder以便公开公开其Configuration属性.
When I'm developing locally using VS Code, I'm going to use port 3000 because I'm a hipster. The non-hipsters want it on port 8080 on the server. That's cool, we got this. Microsoft docs give me the following example:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true)
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.Configure(app =>
{
app.Run(async (context) => await context.Response.WriteAsync("Hi!"));
})
.Build();
host.Run();
}
I don't want to use hosting.json
. Why would I want that? I have this appsettings.{environment}.json
file for exactly this situation. Sweet, I'll just paste that bad boy in
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddCommandLine(args)
.Build();
What the compiler error? env
does not exist in the current context. It only exists in the Startup.cs
file--which is not called on startup, but called from the startup file, Program.cs
, with sorcery.
So, how do I solve this problem? How can I store my environment-specific hosting settings in my environment-specific appsettings.json
, and subsequently use it in while building my environment-specific web host via the WebHostBuilder
in Program.cs
?
This is possible. Expanding on the answer given here, by creating the WebHostBuilder and ConfigurationBuilder in the Program.cs, it is possible to have access to the host environment and then configure the host URL and port in environment-specific appsettings files.
Assuming an appsettings.json and an apppsettings.Development.json file each with the following:
"hostUrl": "http://*:<port number here>"
Modify the Main with the following:
public static void Main(string[] args)
{
var host = new WebHostBuilder();
var env = host.GetSetting("environment");
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env}.json", optional: true)
.AddEnvironmentVariables();
var configuration = builder.Build();
host.UseKestrel()
.UseUrls(configuration["hostUrl"])
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build()
.Run();
}
Using this code, the Startup.cs will still need to declare its own ConfigurationBuilder in order to publicly expose its Configuration property.
这篇关于使用应用程序设置来驱动特定于环境的设置,例如UseUrls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!