Home > database >  .net6.0 c# shared appsettings.json
.net6.0 c# shared appsettings.json

Time:05-09

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 centralise 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 behaviour 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) retun null..... very wierd...

this is the code in the program.cs

            //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);

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 to a dotnet run.

CodePudding user response:

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($"..\\SharedSettings\\sharedsettings.{environment}.JSON", true, true)
                .AddJsonFile($"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:

CodePudding user response:

RESOLVED!!!

            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 link enter link description here

it was all to do with the base path

  • Related