App Services - Web App (Linux Container) - Connection strings not working

Matt Fricker 0 Reputation points
2024-12-07T10:23:50.1133333+00:00

I have a .NET 8 API that I have deployed to Azure App Services - It is a Web App (Linux Container). The API endpoints work except for endpoint that involve a database call. Swagger shows up as expected (as I have configured the app in development mode). However, I cannot seem to configure or change the connection strings via Azure. In my local environment I use user secrets. Here is an extract or example of my appsettings.json file:

{
  "Logging": {
    "LogLevel": {
      "Default": "Error",
      "Microsoft": "Warning",
      "MyApp.Api": "Information",
      "MyApp.Core": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlConnection": "USER SECRETS"
  }
}

In Azure I have configured the Connection strings via: Settings > Environment variables > Connection strings [tab] With the following:

[
  {
    "name": "SqlConnection",
    "value": "{MY PRODUCTION CONNECTION STRING HERE}",
    "type": "SQLServer",
    "slotSetting": false
  }
]

I have tried to change the type, between SQLServer, SQLAzure and Custom, but that has no effect.

I have added logging in an attempt to read and display the connection string when I make an API call that involves calling the database. In the logs it displays USER SECRETS so it looks like the connection string that I have applied in the Azure Portal is not being applied.

My original connection string name was Default but I changed that to SqlConnection but that did not work either.

What am I doing wrong, should I be approaching this differently. Docker and Containers always seem to give me headaches.

Help!

Developer technologies | ASP.NET | ASP.NET Core
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-12-09T07:13:20.9566667+00:00

    Hi Matt Fricker,

    The connectionString with name "SqlConnection" you configured in Azure portal can be access via environment variable in the codes like following:

    var MyConnString = builder.Configuration.GetConnectionString("SqlConnection");
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.