Use appsettings to drive environment specific settings such as UseUrls

放肆的年华 提交于 2019-12-22 05:13:55

问题


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?


回答1:


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.




回答2:


Or, starting from .NET Core 2.0, when creating the default IWebHostBuilder that will build the IWebHost implementation, you can use

return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(configuration)
            .ConfigureAppConfiguration((builderContext, config) =>
            {
                // nb: here you may clear configuration(s) loaded so far
                var env = builderContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);
            })
            .UseStartup<Startup>();


来源:https://stackoverflow.com/questions/43254585/use-appsettings-to-drive-environment-specific-settings-such-as-useurls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!