Search code examples
azureazure-web-app-serviceconnection-stringasp.net-core-3.1

App Service: Not able to read connection string setting from App settings


What I am trying to achieve

Hello, I have dotnetcore web Api which is running in Azure App Service server farm. I am trying to read the connection string from app service configuration app settings.

What I have tried so far

  • Runtime version: netcoreapp3.1 version-2.31.0.1
  • Hosting environment: Azure App Service

Below is how I am trying to read the connection string settings

var configValue = (ConfigurationManager.AppSettings["PNLDBConnectionString"] ?? String.Empty).ToString();

I have added the connection string in app service configuration section as below

enter image description here

As well have added a app settings key as below

enter image description here

But in either way, if I am removing the connection string key from app.config and deploying; then while trying to run any API endpoint throws below error which essentially means the it's not able to read the said connection string property

enter image description here

Any idea, what I could be missing here? Please suggest.

EDIT:

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>

    <add key ="Encrypted" value="true"/>
    <add key ="PNLDBConnectionString" value="connection string value"/>

  </appSettings>
</configuration>

Solution

  • Looking at the Kudu portal, under environment variables I could see the settings as

    PNLDBConnectionString = xxxxxxxxxx
    APPSETTING_PNLDBConnectionString = xxxxxxxxx
    

    Using, Environment.GetEnvironmentVariable("APPSETTING_PNLDBConnectionString") did the trick actually. Had to modify the code line a bit like

    var configValue = ConfigurationManager.AppSettings["PNLDBConnectionString"] 
                     ?? Environment.GetEnvironmentVariable("APPSETTING_PNLDBConnectionString")
                     ?? String.Empty;