WynDiesel
WynDiesel

Reputation: 1214

Add a linked appsettings.json

I have several projects that share the same appsettings.json file.

When I add an appsettings.json file to each project on it's own, my projects starts up and loads configuration from these files.

I would like to have one file, that I link (Add -> Existing -> Add as Link), so that I have one single file to maintain.

Doing so, changes my .proj file to include the files :

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

When I do a build, I can see both the appsettings in the bin\Debug\netcoreapp3.1 directory.

My startup:

public static void Main(string[] args)
{
    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
    CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                var lConfiguration = hostContext.Configuration;

                ServiceConfiguration lOptions = lConfiguration.GetSection(nameof(ServiceConfiguration)).Get<ServiceConfiguration>();
                services.AddSingleton(lOptions);

            })
            .UseSerilog((hostingContext, loggerConfiguration) =>
                    loggerConfiguration.ReadFrom.Configuration(hostingContext.Configuration)
                );

However, when I debug my project, and I inspect the Configuration object, I can see all the configuration sources, but the appsettings source is empty.

I am confused, as the appsettings file is being built, but the configuration is not picking it up.

What am I missing?

Upvotes: 4

Views: 2007

Answers (2)

Miroslav Popovic
Miroslav Popovic

Reputation: 12128

There is one more way you can control from where the application will start. You can modify launchSettings.json file to add workingDirectory setting to your launch profile. Just point it to your bin output folder:

{
  "profiles": {
    "Project1": {
      "commandName": "Project",
      "workingDirectory": "bin/Debug/net6.0",
      "dotnetRunMessages": true,
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      }
    }
  }
}

Upvotes: 6

WynDiesel
WynDiesel

Reputation: 1214

The problem was that the working directory in .NET Core defaults to the source folder, and not the binaries folder. This means that it was looking for the appsettings.json file in my source folder, which would be there if I added an appsettings file manually, but when I linked it, it was not in the source folder.

The issue was resolved by changing the default working directory to the binary folder by doing this :

Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

Upvotes: 5

Related Questions