.NET6.0 C#共享AppSettings.json

发布于 2025-01-27 08:35:16 字数 1929 浏览 3 评论 0原文

我正在使用一个多租户.NET6.0解决方案,该解决方案有许多嵌入式项目

以保持低位维护,我想集中和使用 /链接一个单个AppSettings。{Environment}。每个项目参考适当的文件。

因此,我目前正在尝试将其设置在控制台应用程序(将被部署为WebJob)中,并且在Dotnet Run中,我会获得非常奇怪的行为。

控制台应用中,我在项目文件中的sharedSettings.json中添加了一条路径

<ItemGroup>
        <Content Include="..\SharedSettings\sharedsettings.development.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="..\SharedSettings\sharedsettings.production.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="..\SharedSettings\sharedsettings.staging.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
</ItemGroup>

在此 必需的配置设置。

但是,当我进行dotnet运行时,所需的JSON路径(来自同一文件)的AppSettings返回null。

这是我使用过文件的程序中的代码

//load the correct environment variables, i.e. Sevelopment / Staging / Production appsettings.JSON
string deployJSON = $"sharedsettings.{environment}.json";
string[] localPath = new string[] { "..", "SharedSettings", deployJSON };
var localJSON = Path.Combine(localPath);
Console.WriteLine(Directory.GetCurrentDirectory()); 

            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(localJSON, true, true)
                .AddJsonFile(deployJSON, true, true)
                .AddEnvironmentVariables()
                .Build();

string queueName = Configuration.GetValue<string>("ConnectionStrings:ServiceBusName");
string serviceBusConnection = Configuration.GetValue<string>("ConnectionStrings:ServiceBusConnectionString");

Console.WriteLine(queueName);
Console.WriteLine(serviceBusConnection);

。CSSSISTS确认我可以到达文件,但是当我尝试运行dotnet运行时结果为何无效。

I'm working away at a multi-tenant .net6.0 solution which has a number of embedded projects

to keep maintenance low, I want to centralize and use / link one single appsettings.{environment}.JSON for the entire solution and have each project reference the appropriate files.

so I'm currently trying to set this up in a console app (which will be deployed as a webjob) and I'm getting a really weird behavior with dotnet run.

in this console app, I have added a path to the sharedsettings.json in the project file, i.e.

<ItemGroup>
        <Content Include="..\SharedSettings\sharedsettings.development.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="..\SharedSettings\sharedsettings.production.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Include="..\SharedSettings\sharedsettings.staging.json">
            <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
</ItemGroup>

And when I run the app using the play button i.e. via a deploy build in Visual Studio I can read the contents of the file and establish the required config settings.

However, when I do a dotnet run, the appsettings for the required JSON path (from the same file) returns null.

This is the code in the program.cs

//load the correct environment variables, i.e. Sevelopment / Staging / Production appsettings.JSON
string deployJSON = 
quot;sharedsettings.{environment}.json";
string[] localPath = new string[] { "..", "SharedSettings", deployJSON };
var localJSON = Path.Combine(localPath);
Console.WriteLine(Directory.GetCurrentDirectory()); 

            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(localJSON, true, true)
                .AddJsonFile(deployJSON, true, true)
                .AddEnvironmentVariables()
                .Build();

string queueName = Configuration.GetValue<string>("ConnectionStrings:ServiceBusName");
string serviceBusConnection = Configuration.GetValue<string>("ConnectionStrings:ServiceBusConnectionString");

Console.WriteLine(queueName);
Console.WriteLine(serviceBusConnection);

I have used file.exists to confirm I can get to the file, but I'm stumped as to why the results are null when I attempt to a dotnet run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满栀 2025-02-03 08:35:16

然后,我可以将sharedSettings.json作为本地文件引用

我现在所做的一切;

  1. 在部署中包括外部文件
  <ItemGroup>
    <Content Include="..\SharedSettings\sharedsettings.development.JSON" Link="sharedsettings.development.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="..\SharedSettings\sharedsettings.JSON" Link="sharedsettings.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="..\SharedSettings\sharedsettings.production.JSON" Link="sharedsettings.production.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="..\SharedSettings\sharedsettings.staging.JSON" Link="sharedsettings.staging.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  1. 添加外部文件(作为链接)
    [在这里] https:https:https: //Andrewlock.net/cluding-linked-files-from-outside-the-projection-directory-in-asp-net-core/

  2. 在配置构建器中引用文件,就像是本地文件一样

            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile($"..\\SharedSettings\\sharedsettings.{environment}.JSON", true, true)
                .AddJsonFile($"sharedsettings.{environment}.json", true, true)
                .AddEnvironmentVariables()
                .Build();

完美,但是dotnet运行仍然具有null值引用,除非我很难添加共享集群文件,即将它们嵌入每个项目中,而不是作为链接。 :生气的:

then i shuold be able to reference the sharedsettings.json as a local file

all i'm doing now is;

  1. include the external files in the deployment
  <ItemGroup>
    <Content Include="..\SharedSettings\sharedsettings.development.JSON" Link="sharedsettings.development.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="..\SharedSettings\sharedsettings.JSON" Link="sharedsettings.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="..\SharedSettings\sharedsettings.production.JSON" Link="sharedsettings.production.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include="..\SharedSettings\sharedsettings.staging.JSON" Link="sharedsettings.staging.JSON">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  1. add an external file (as a link)
    [here]https://andrewlock.net/including-linked-files-from-outside-the-project-directory-in-asp-net-core/

  2. reference the file in the configuration builder as if it's a local file

            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(
quot;..\\SharedSettings\\sharedsettings.{environment}.JSON", true, true)
                .AddJsonFile(
quot;sharedsettings.{environment}.json", true, true)
                .AddEnvironmentVariables()
                .Build();

the build is perfect, however dotnet run still has the NULL value references unless i hard add the sharedsettings files i.e. embed them in each project not as a link. :angry:

山川志 2025-02-03 08:35:16
            Configuration = new ConfigurationBuilder()
            .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
            //.SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile($"..\\SharedSettings\\sharedsettings.{environment}.JSON", true, true)
            .AddJsonFile($"sharedsettings.{environment}.json", true, true)
            .AddEnvironmentVariables()
            .Build();

similar issue to this one

这与基本路径有关。

            Configuration = new ConfigurationBuilder()
            .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
            //.SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(
quot;..\\SharedSettings\\sharedsettings.{environment}.JSON", true, true)
            .AddJsonFile(
quot;sharedsettings.{environment}.json", true, true)
            .AddEnvironmentVariables()
            .Build();

similar issue to this one.

It was all to do with the base path.

蓝咒 2025-02-03 08:35:16

这是一个较晚的答案,但是在尝试了不同的方法之后,我在项目的.csproj文件中最终提出了这一点:

<Target Name="CopyAppSettings" BeforeTargets="BeforeBuild">
    <Copy SourceFiles="path_to_shared_appsettings/appsettings.json" DestinationFolder="./" SkipUnchangedFiles="true" />
</Target>

这将在构建之前每次将AppSettings复制到项目文件夹中。

This is a late answer, but after trying different methods I ended up with this in my Project's .csproj file:

<Target Name="CopyAppSettings" BeforeTargets="BeforeBuild">
    <Copy SourceFiles="path_to_shared_appsettings/appsettings.json" DestinationFolder="./" SkipUnchangedFiles="true" />
</Target>

This copies the appsettings to the project folder every time before a build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文