由于 appsettings.json 就足够了,hosting.json 有什么意义 [英] What's the point of hosting.json since appsettings.json is sufficient

查看:27
本文介绍了由于 appsettings.json 就足够了,hosting.json 有什么意义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET Core 2 Web API 应用程序中,我可以使用 appsettings.json 覆盖配置 urls,但在 官方文档 他们引入了额外的文件hosting.json",为什么?增加复杂性有什么意义?

In .NET Core 2 Web API app, I could override configuration urls using appsettings.json, but in the official docs they introduced extra file "hosting.json", Why? What's the point of adding complexity?

以下代码使用 appsettings.json 完全可以工作:

Below code is fully working using appsettings.json:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory()) //see Side note below
            .AddJsonFile("appsettings.json", optional: true)
            .AddCommandLine(args)
            .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseConfiguration(config)
            .UseStartup<Startup>()
            .Build();
    }
}

appsettings.json 内容:

appsettings.json content:

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "urls": "http://*:5005/"
}

旁注:注释 .SetBasePath(Directory.GetCurrentDirectory()) 将使 VS 2017 调试模式保持运行(意味着应用 launchSettings.json 和自动启动 url)否则不会.我猜它与 CreateDefaultBuilder 实施.

Side note: Commenting .SetBasePath(Directory.GetCurrentDirectory()) will keep VS 2017 debug mode operational (means apply launchSettings.json, and auto launch url) otherwise it won't. I guess its related to the CreateDefaultBuilder implementation.

推荐答案

我认为,hosting.json 是一个专门用于 asp.net 核心应用程序托管的配置文件.(如果您更了解托管)

I think, hosting.json is a configuration file used specifically for asp.net core application hosting. (if you know more about hosting)

WebHostBuilder 直接将其键映射到 hosts.json 文件,它无法像我们在正常配置设置中那样加载 config 部分.

WebHostBuilder directly maps its keys with the hosting.json file and it doesn't have the capability to load the config section as we do in normal configuration settings.

根据您帖子中的链接

使用配置来配置主机.在下面的例子中,主机配置可选择在托管.json 文件中指定.任何从 hosts.json 文件加载的配置可能会被覆盖命令行参数.

Use Configuration to configure the host. In the following example, host configuration is optionally specified in a hosting.json file. Any configuration loaded from the hosting.json file may be overridden by command-line arguments.

如果我们明确使用 hosting.json,那么可以使用 dotnet 命令修改 WebHostBuilder 配置.

If only we explicitly us hosting.json then the WebHostBuilder configurations can be modified using dotnet command.

例如

dotnet run --urls "http://*:8080"

dotnet run --urls "http://*:8080"

这将覆盖来自 hosts.json 文件的 url.

this will override the urls from hostings.json file.

希望这可以提供一些想法.

Hopefully, this may give some idea.

PC:hosting.json 可以像 myappsettings.json 一样重命名它可以有配置和 Web Host Builder 配置.

PC: hosting.json can be renamed like myappsettings.json it can have configuration and Web Host Builder configuration.

这篇关于由于 appsettings.json 就足够了,hosting.json 有什么意义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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