0

I'm trying to publish my .NET 7 Web API on Azure as App Service + Database. Locally I execute the following code to get appsettings.json working in my dotnet publish (-ed) app:

var builder = WebApplication.CreateBuilder(args);

builder.Configuration
    .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "src", "Web"))
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
    .Build();

For some reason otherwise it only works in IDE and won't work in dotnet publish'ed app.

But on the Azure I get:

Unhandled exception. System.IO.DirectoryNotFoundException: /home/site/wwwroot/src/Web/
2023-04-13T12:29:00.959798795Z    at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
2023-04-13T12:29:00.959811695Z    at Microsoft.Extensions.Configuration.FileConfigurationExtensions.SetBasePath(IConfigurationBuilder builder, String basePath)
2023-04-13T12:29:00.966010289Z    at Program.<Main>$(String[] args) in /home/runner/work/flitch-backend/flitch-backend/src/Web/Program.cs:line 14
2023-04-13T12:29:04.369525714Z /opt/startup/startup.sh: line 17:    69 Aborted                 (core dumped) dotnet Web.dll

I think the problem might be in the folder structure, my folder structure is like that:

Application Application/src Application/src/Web (the project I run and the directory where appsettings.json reside)

Here's the github repo of the project I try to publish: https://github.com/Rostand-Corp/flitch-backend/tree/develop

appsettings.json properties are: Build action: Content Copy to output directory: Copy always

I have tried changing changing path to just current directory, but it won't work

bookuhah
  • 9
  • 6
  • I have a feeling that this is the right solution https://stackoverflow.com/questions/39157781/the-configuration-file-appsettings-json-was-not-found-and-is-not-optional/52461586#52461586 – Maytham Fahmi Apr 13 '23 at 13:44
  • If the other answer you point to is absolutely correct I would close this question as duplicate – Maytham Fahmi Apr 13 '23 at 13:46
  • I have tried the answer you provided the link for, but it didn't help (well, it is a good practice anyway, I guess) You may close the question, I guess, but I'd leave it. It may be useful since the one I have provided is not related to Azure and I suspected only Azure's file system to cause the problem – bookuhah Apr 13 '23 at 13:51
  • Does this answer your question? [Published .Net Core 2 application does not read appsettings.json file](https://stackoverflow.com/questions/50578013/published-net-core-2-application-does-not-read-appsettings-json-file) – Maytham Fahmi Apr 13 '23 at 14:14
  • @MaythamFahmi yes – bookuhah Apr 13 '23 at 15:34

1 Answers1

0

I was able to find the solution here.

I was running dotnet Web.dll from my source code directory and therefore Directory.GetCurrentDirectory() was been evaluating to the source code directory.

Using this helped:

cd *myPublishedAppDirectory*
dotnet Web.dll 

Instead of this:

dotnet *myPublishedAppDirectory*/Web.dll

Directory.GetCurrentDirectory() is now evaluated correctly.

user16217248
  • 3,119
  • 19
  • 19
  • 37
bookuhah
  • 9
  • 6