无法读取 dotnet 核心中链接的 appsettings.json 文件中的值 [英] Values from linked appsettings.json file in dotnet core cannot be read

查看:24
本文介绍了无法读取 dotnet 核心中链接的 appsettings.json 文件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 aspnetcore 2.0 项目中,我试图在我的 Web 应用程序和我的几个 xunit 测试项目中设置一个共享的 appsettings.json 文件.

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.

首先,当我将 appsettings.json定期"单独添加到我的项目时,一切正常.但是,当我尝试从不同路径添加单个引用/链接文件时,在运行时不会读出这些值.

First off, everything works fine when I add the appsettings.json "regularly" to my projects individually. However, when I attempt to add a single reference/linked file from a different path, the values aren't read out during runtime.

为了进行设置,对于每个项目,我将链接文件添加到 .csproj:

To set this up, for each project I add the linked file to the .csproj:

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

并且在每个构建中,我可以验证它是否很好地输出到 /bin/Debug/netcoreapp2.0 目录:

And on each build, I can verify it is output nicely to the /bin/Debug/netcoreapp2.0 directory:

问题是,在读取时,无论是通过我的 webapi 的默认启动还是在测试中,设置值始终为空.我正在使用强类型配置.在 webapi 项目中,我使用的是 WebHost.CreateDefaultBuilder() 与 aspnetcore 2.0 约定(在幕后查看 hostingEnvironment.EnvironmentName,dotPeek 告诉我).

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. 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).

但在测试项目的情况下,也会发生同样的情况.这一切都可以通过我的全局测试装置中的以下示例来解释:

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'");

问题在上述示例的最后一行注释.同样,当我直接将 appsettings.json 文件添加到项目根目录而不是将其添加为共享文件夹的链接引用时,它可以完美运行.我假设我必须以不同的方式更改 .csproj 配置或使用不同的 .SetBasePath(???).但对于两者,我都在为如何实现它而苦苦挣扎.

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. I am assuming I have to either change the .csproj configuration differently OR use a different .SetBasePath(???). But for both I'm struggling on how to achieve it.

问题:我该怎么做才能使链接的 appsettings 文件在我的所有项目中都能正确读取?

The question: What can I do to make the linked appsettings file to be read properly in all my projects?

注意我找到了其他几个帖子,但找不到任何具有匹配答案的帖子,特别是针对 dotnetcore.

N.B. I found several other posts, but couldn't find any with a matching answer, specifically for dotnetcore.

推荐答案

在我在 Program 中添加一些代码后它开始工作(感谢@mr_squall 的指导..):

It started working after i added some code in Program (thanks @mr_squall for direction..):

     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>();
                });

这篇关于无法读取 dotnet 核心中链接的 appsettings.json 文件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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