Net Core application not reading ASPNETCORE_ENVIRONMENT value?

374 views Asked by At

I deployed an ASP.NET Core 7 application to Linux Web Application in Azure.

When I access the URL I get an Application Error and the Logs shows:

System.IO.FileNotFoundException: 
The configuration file 'settings..json' was not found and is not optional. 

It seems it is missing the Environment value so it should be:

settings.production.json

In the Azure Application Service Configuration I have:

[
  {
    "name": "ASPNETCORE_ENVIRONMENT",
    "value": "production",
    "slotSetting": false
  }
]

And the application Program.cs code is:

Serilog.Log.Logger = new 

Serilog.LoggerConfiguration()
  .WriteTo.Console(LogEventLevel.Verbose)
  .CreateBootstrapLogger();

try {

  Serilog.Log.Information("Starting up");

  WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions { 
    Args = args, 
    WebRootPath = "webroot" 
  });

  builder.Configuration
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("settings.json", false, true)
    .AddJsonFile($"settings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", false, true)
    .AddEnvironmentVariables();

  // Remaining code 

Am I doing something wrong or something change in Net 7?

1

There are 1 answers

0
Jason Pan On

In short, this problem occurs because the settings.production.json file was not included at the time of release.

We can verify this by uploading the 'settings.production.json' file to the scm site. The URL is https://your_appname_azurewebsites.net/newui .

enter image description here

Solution:

  1. Official doc : Include files
  2. Sampleļ¼š Use ResolvedFileToPublish in ItemGroup