The configuration file 'appsettings.json' cannot be found when running app on dotnet:2.1-runtime
Itfly opened this issue · comments
I use console app to host my grpc server, it reads configuration like below code:
var env = context.HostingEnvironment;
builder.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
This app works well on both my local machine (dotnet 2.1.104) and docker with dotnet:latest
, except on docker with dotnet:2.1-runtime
which throws the below exception:
write(9, "System.IO.FileNotFoundException:"..., 883System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is '/app/appsettings.json'.
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.RunConsoleAsync(IHostBuilder hostBuilder, CancellationToken cancellationToken)
I created the issue first on dotnet/dotnet-docker, and @MichaelSimons said maybe this is Configuration
's issue, so i create it here in order to find the root cause.
My guess is that the file is simply missing from your publish output. The repro you attached has
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
This would mean the files is only in the output of dotnet build
, not dotnet publish
.
Try updating it to this:
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ <CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>
@natemcmaster THe file is in the publish folder and also in the running container. It's just not working on linux vm or docker. I used strace
to trace the system calls there's no open(/app/appsettings.json) call before it throws exception.
Thank you for the repro. This helped a bunch. There is a gnarly bug in .NET Core caused by the new rollforward mechanism that lifts you from .NET Core 2.0 to 2.1. I've filed https://github.com/dotnet/corefx/issues/29298 with more details. The bug isn't actually in aspnet/Configuration, so I'm going to close in favor of that bug.
Until this is resolved, I recommend changing your application to compile for .NET Core 2.1 instead, or run the app in microsoft/dotnet:2.0-runtime
. It appears the mixing of <TargetFramework>netcoreapp2.0</TargetFramework>
and microsoft/dotnet:2.1-runtime
is what has some bugs in it. Please let me know if switching to <TargetFramework>netcoreapp2.1</TargetFramework>
doesn't work.
Thanks, microsoft/dotnet:latest
works well