eXavier
eXavier

Reputation: 4981

aspnet core appsettings.json loading

I have ASP.NET Core (2.1) project that has appsettings.json. I use WebHost.CreateDefaultBuilder(). The appsettings.json file has following configuration in File Properties:

Build Action: Content
Copy to Output Directory: Do not copy

After build the appsettings.json ends up in bin\Debug\netcoreapp2.1\MyProj.runtimeconfig.json. The ASP.NET Core runtime loads it fine.

I created WebJobs (for .Net Core 2.1) and wanted to do the same - set Build Action to Content and let it loaded. In the Main() of Program.cs I have code like

var builder = new HostBuilder()
  ...
.ConfigureAppConfiguration(b =>
{
    var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

    b.SetBasePath(Directory.GetCurrentDirectory());
    b.AddJsonFile("appsettings.json", false, true);
    b.AddJsonFile($"appsettings.{environment}.json", true, true);
    b.AddEnvironmentVariables();

    // Adding command line as a configuration source
    if (args != null)
    {
        b.AddCommandLine(args);
    }
}

But the runtime tries to load appsettings.json (instead of MyWebJobProj.runtimeconfig.json). So I had to set Build Action to None and Copy to Output Directory to Always. However I would prefer the same approach like in ASP.NET Core - it handles somehow the file name transformation. Although in WebHost.CreateDefaultBuilder() is basically the same code like I have in my WebJob. What does the magic file name transformation in the configuration and why it works only in one type of project?

Upvotes: 2

Views: 490

Answers (1)

Igor Goyda
Igor Goyda

Reputation: 2017

The file [ProjName].runtimeconfig.json has a completely different meaning than appsettings.json. Ensure that appsettings.json was copied to output (set 'Copy to output' to 'always' or 'newer').

Upvotes: 1

Related Questions