使用应用程序设置来驱动特定于环境的设置,例如UseUrls [英] Use appsettings to drive environment specific settings such as UseUrls

查看:77
本文介绍了使用应用程序设置来驱动特定于环境的设置,例如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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆