简体   繁体   English

无法读取 dotnet 核心中链接的 appsettings.json 文件中的值

[英]Values from linked appsettings.json file in dotnet core cannot be read

In an aspnetcore 2.0 project I'm trying to setup a single, shared appsettings.json file throughout my web application and several of my xunit test projects.在一个 aspnetcore 2.0 项目中,我试图在我的 web 应用程序和我的几个 xunit 测试项目中设置一个共享的 appsettings.json 文件。

First off, everything works fine when I add the appsettings.json "regularly" to my projects individually.首先,当我将 appsettings.json “定期”单独添加到我的项目时,一切正常。 However, when I attempt to add a single reference/linked file from a different path, the values aren't read out during runtime.但是,当我尝试从不同路径添加单个引用/链接文件时,在运行时不会读出这些值。

To set this up, for each project I add the linked file to the .csproj:为了进行设置,对于每个项目,我将链接文件添加到 .csproj:

<ItemGroup>
    <Content Include="..\Shared\appsettings.json">
        <Link>appsettings.json</Link>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>

And on each build, I can verify it is output nicely to the /bin/Debug/netcoreapp2.0 directory:在每个构建中,我可以验证它是否很好地输出到/bin/Debug/netcoreapp2.0目录:

输出目录截图

The problem is that when reading it out, either through the default Startup of my webapi or in the tests, the settings values are always null.问题是,在读取它时,无论是通过我的 webapi 的默认启动还是在测试中,设置值始终为空。 I'm using a strong typed configuration.我正在使用强类型配置。 In the webapi project, I'm using the WebHost.CreateDefaultBuilder() with the aspnetcore 2.0 conventions (which under the hood looks at hostingEnvironment.EnvironmentName , dotPeek tells me).在 webapi 项目中,我将WebHost.CreateDefaultBuilder()与 aspnetcore 2.0 约定一起使用(dotPeek 告诉我,它在hostingEnvironment.EnvironmentName查看了hostingEnvironment.EnvironmentName )。

But also in the case of the test projects, the same occurs.但在测试项目的情况下,也会发生同样的情况。 Which all can be explained by the following example in my global test fixture:这一切都可以通过我的全局测试装置中的以下示例来解释:

var builder = new ConfigurationBuilder()
    // .SetBasePath(???)
    .AddJsonFile("appsettings.json");

var configuration = builder.Build();

var settings = new FooSettings();
configuration.GetSection("FooSettings").Bind(settings);

if (settings == null) throw new Exception("Could not find or parse 'appsettings.json'");

// The problem: always fails on this check
if (string.IsNullOrEmpty(settings.Bar)) throw new Exception("Could not find or parse 'FooSettings'");

The problem is commented on the last line in the above sample.问题在上述示例的最后一行进行了注释。 Again, it works perfect when I directly add the appsettings.json file to the project root, instead of adding it as a linked reference to a shared folder.同样,当我直接将 appsettings.json 文件添加到项目根目录而不是将其添加为共享文件夹的链接引用时,它可以完美运行。 I am assuming I have to either change the .csproj configuration differently OR use a different .SetBasePath(???) .我假设我必须以不同的方式更改 .csproj 配置或使用不同的.SetBasePath(???) But for both I'm struggling on how to achieve it.但对于两者,我都在努力实现它。

The question: What can I do to make the linked appsettings file to be read properly in all my projects?问题:我该怎么做才能使链接的appsettings 文件在我的所有项目中都能正确读取?

NB I found several other posts, but couldn't find any with a matching answer, specifically for dotnetcore.注意我发现了其他几篇文章,但找不到任何具有匹配答案的帖子,特别是针对 dotnetcore。

It started working after i added some code in Program (thanks @mr_squall for direction..):我在Program添加了一些代码后它开始工作(感谢@mr_squall 的指导..):

     public static void Main(string[] args)
        {
            CreateHostBuilder(args)
#if !DEBUG
                .UseContentRoot(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
#endif
                .Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM