【问题标题】:Why is WebHost.CreateDefaultBuilder.UseConfiguration not passing config to the application为什么 WebHost.CreateDefaultBuilder.UseConfiguration 不将配置传递给应用程序
【发布时间】:2019-01-03 12:00:41
【问题描述】:

WebHost.CreateDefaultbuilder(args).UseConfiguration,默认情况下应该使用 webhost 和应用程序的配置。 ConfigureAppConfiguration 仅用于配置应用程序,而不是主机。

下面的代码以前可以运行,没有问题。 从 .net core 2.1 更新到 2.2 并更新一些 nuget 包以支持 .net core 2.2 后,代码不再工作。

我确实发现了一个肮脏的黑客,但我想摆脱它,因为我担心我将来可能会得到一些不需要的结果。

return WebHost.CreateDefaultBuilder(args)
    .UseContentRoot(AppDomain.CurrentDomain.BaseDirectory)
    .UseConfiguration(ConfigurationService.Config)
    // Dirty hack to temp fix my problem
    //.ConfigureAppConfiguration(configuration => configuration.AddConfiguration(ConfigurationService.Config))
    .UseStartup<Startup>()
    .UseSerilog()
    .Build();

ConfigurationService.Config 是在调用 webhost 之前的静态 ConfigurationRoot 构建。 ConfigurationService 也开始记录,确定一些目录和其他事情。

返回的配置包含:环境、appsettings.json 文件、custom.json 文件和命令行提供程序。 我知道这适用于主机,因为 custom.json 文件中的 kestrel 配置。 在所有更改之前,如果我向 IConfigurationRoot 添加依赖项,我也会看到相同的提供程序。

现在我在 IConriguration 依赖项上看到了默认的 .net 核心提供程序 (chained, environment, appsettings, appsetting.development, commandline)(最重要的,我的custom.json文件不在这)。

我已经尝试过不使用静态 iconfiguration root,并以返回 WebHost 的相同方法构建它,但无济于事,因此它与我的 ConfigurationService 无关。

【问题讨论】:

    标签: c# asp.net-core configuration


    【解决方案1】:

    好吧,我错了。

    我试图通过我的一种方法从服务提供者那里获取选项。但由于应用程序尚未启动,我首先必须手动构建 serviceCollection。

    【讨论】:

    • 您好,请接受您的回答以关闭冷柜!?
    猜你喜欢
    • 2015-08-29
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2021-03-06
    相关资源
    最近更新 更多