Search code examples
.netconsole-applicationmulti-tenantappsettingssingle-source

.net6.0 c# shared appsettings.json


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 = $"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.


Solution

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

    It was all to do with the base path.