Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 传递到启动模块的奇怪I配置_C#_Asp.net Core_Configuration - Fatal编程技术网

C# 传递到启动模块的奇怪I配置

C# 传递到启动模块的奇怪I配置,c#,asp.net-core,configuration,C#,Asp.net Core,Configuration,我有一个ASP.NET核心应用程序,它在Program.cs文件(“Main”方法)中构建自定义配置。它就是这样构建的: var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)

我有一个ASP.NET核心应用程序,它在Program.cs文件(“Main”方法)中构建自定义配置。它就是这样构建的:

var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)

        // custom config file
        .AddJsonFile("custom.json", optional: false, reloadOnChange: false)
                .Build();

    BuildWebHost(args, configuration).Run();
发生的情况是,beforeBuildWebHost调用配置正确地由两部分组成——appsettings.json和custom.json

当我在启动构造函数中捕获执行时,custom.json配置就消失了:它不在那里。在传递给Startup-ctor的配置中还有很多其他分支,但是没有custom.json分支

我可能做错了什么

更新:

public static IWebHost BuildWebHost(string[] args, IConfiguration config)
{
    var ret = WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(config)
        .UseStartup<Startup>()
        .Build();

    return ret;
}
public Startup(IConfiguration configuration)
{
    this.Configuration = configuration;
}

我还没有弄清楚为什么
程序中的
UseConfiguration
不起作用,也没有在哪里记录何时/如何使用它。但是,您可以将
UseConfiguration
替换为
ConfigureAppConfiguration
,以附加到
CreateDefaultBuilder
添加的默认提供程序列表中

例如:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(builder => builder
                .AddJsonFile("foo.json"))
            .UseStartup<Startup>();
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(生成器=>builder
.AddJsonFile(“foo.json”))
.UseStartup();

您是否检查了
custom.json
文件的属性,以确保其
复制到输出
设置为
始终复制
复制(如果较新)
?是的,这不会影响它。目前它被设置为“buildaction:Content”,“Copy to output directory:Copy always”我怀疑您的
配置
变量没有被传递,或者在传递到
启动
类后您没有正确分配它。它不是API的正式部分,但如果在启动时将鼠标悬停在
IConfiguration
上,它为
\u提供程序列出了什么?有5个提供程序:ChainedConfigurationProvider、2个JSONConfigurationProvider(没有一个是我的自定义.json)、EnvironmentVariablesConfigurationProvider、,和CommandLineConfigurationProvider。如果你的
启动
类有构造函数,你能在你设置
IConfiguration
成员的任何地方发布它吗?这个github答案中似乎有更多的信息,我希望会有文档(某处…):