3

I am totally new to using Azure services. I've been trying to deploy a C# / ASP.NET Core 7 app I created that relies on Postgres as the database and Entity Framework as ORM.

I used App Services (Linux) to deploy my app from a GitHub Action and the Postgres flexible server as the database.

However my app is unable to load the Connection string I added into the "Settings > Configuration > Connection strings".

Here is the code I use to load it in my Program.cs file:

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseNpgsql(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

The app works successfully when used locally w/ docker.

I tried to use Microsoft Key Store but it failed. Also tried to load connection string from env instead of configuration file. Last thing I have done is put into my appsettings.json a section ConnectionStrings with a DefaultConnection and an empty string to it, and that didn't work

kittyyy
  • 56
  • 4

1 Answers1

3

If you are deploying to Azure App Service there are basically two options to ship application settings:

  1. make sure that your local appsetting.json is also copied to output directory (check file properties)

  2. you can setup application settings including Connection Strings in App Settings tab of you App Service instance

Adalyat Nazirov
  • 1,611
  • 2
  • 14
  • 28
  • i feel like that the appsettings.json file is well copied to the output directory because when i set the DefaultConnection value in this file the program starts, but is unable to connect since it's using the value of my local development database. However when i remove it the app just crashes. It kind of surprise me cause it feel like the Connection String i set is not overwriting properties of the appsettings.json file ! – kittyyy May 20 '23 at 12:14
  • I have the same issue, while 2 should be working, it seems to return null. A workaround I am using now is defining ConnectionStrings__DefaultConnection in Azure's regular application settings. But I'd like to know why setting a connection string isn't working. – Hedgelot Jul 11 '23 at 12:00