2

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?

1 Answer 1

1

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').

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.