'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 1:[1]

  • After Adding New ConnectionString in Azure Portal => Configuration => Application settings Add Connection string in appsettings.json file

     "ConnectionStrings": {
        "DefaultConnection": "Server=SQLAzure; Database=PNLDB; Trusted_Connection=True; MultipleActiveResultSets=true"
      }
    

In Startup.cs, Setup configuration settings to overwrite the environmental variables with appsettings.json.

public IConfiguration Configuration { get; set; }
public Startup()
{
    Configuration = new Configuration()
        .AddJsonFile("appsettings.json")
        .AddEnvironmentVariables();    
}

Configure the DB in Startup

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ProjectContext>(options =>
        {
            var connString = Configuration.Get("PNLDBConnectionString");
            options.UseSqlServer(connString);
        });
}
  • Check the Connection string in KUDU (Control Manager)

Update

var connection = 
    System.Configuration.ConfigurationManager.
    ConnectionStrings["PNLDBConnectionString”].ConnectionString;

Your assembly also needs a reference to System.Configuration.

  • I have found that you added value in connection string section and using app settings in code.
  • You need to add value in App settings in portal

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1