1
votes

I am using Azure App Service. I am trying to add several parameters in section Application settings -> App settings and Connection strings to override values from web.config

I cannot get access to these settings using ConfigurationManager. My project is based on .Net 4.6.

I can get access to these app settings and connection strings using Environment.GetEnvironmentVariable("name").

Also these settings are not visible on page https://.scm.azurewebsites.net/Env.cshtml in sections AppSettings and Connection Strings, only in section Environment variables

I read a lot of articles and haven't not found an answer.
Is it by design or I am doing something wrong?

Update 1 I created a test project in VS 2015 update 3 - Webforms App and it works, but it contains OWIN, by my project does not.

Update 2 Looks like I described my problem incorrectly. ConfigurationManager works, and I can access settings from web.config, but after deployment to Azure I am expecting that values will be substituted by values that I entered in Azure App Service -> Application settings -> App settings and Connection strings. But it does not happen.

My web.config:

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

 <appSettings>
    <add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="dummy" />
 </appSettings>

 <connectionStrings>
    <add name="default" providerName="System.Data.SqlClient" connectionString="empty" />
    <add name="ApolloTI" providerName="System.Data.SqlClient" connectionString="empty" />
    <add name="TestData" providerName="System.Data.SqlClient" connectionString="empty" />
 </connectionStrings>
   ...
</configuration>

Screenshot - Setting in Azure

Update 3 I just created an empty web project and it works, so looks like the problem is somewhere in my code or in project configuration.

1
How exactly are you using the Configuration manager? Include a code snippet, an expected behavior, an actual behavior and a screenshot from your app settings in portal.astaykov
I Updated my post, added detailsVladimir Danilevskyi

1 Answers

0
votes

I can get access to these app settings and connection strings using Environment.GetEnvironmentVariable("name"). Also these settings are not visible on page https://.scm.azurewebsites.net/Env.cshtml in sections AppSettings and Connection Strings, only in section Environment variables

As far as I know, the kudu’s environment page will only show the default appsettings and Connection Strings. It will not contain the webconfig’s appsettings and the appsettings you set in the portal.

According to this article,you will find below information:

App settings For .NET apps, these settings are injected into your .NET configuration AppSettings at runtime, overriding existing settings. Connection strings For each app setting, two environment variables are created; one with the name specified by the app setting entry, and another with a prefix of APPSETTING_. Both contain the same value.

For .NET apps, these connection strings are injected into your .NET configuration connectionStrings settings at runtime, overriding existing entries where the key equals the linked database name. These settings will also be available as environment variables at runtime, prefixed with the connection type. The environment variable prefixes are as follows:

SQL Server: SQLCONNSTR_
MySQL: MYSQLCONNSTR_
SQL Database: SQLAZURECONNSTR_
Custom: CUSTOMCONNSTR_

For example, if a Custom connection string were named Redis, it would be accessed through the environment variable CUSTOMCONNSTR_Redis.

So you could find your appsettings and connectionstring in the environment variable.

And this value will replace the webconfig value when your application is running.

I also write a test demo on my computer by using the ConfigurationManager class, it works well.

So I guess maybe something wrong with your codes.

Here is my test demo, hope it gives you some tips:

public void ReadAllSettings()
{
    string o = "";
    try
    {
        var appSettings = ConfigurationManager.AppSettings;

        if (appSettings.Count == 0)
        {
            o =  "AppSettings is empty.";
        }
        else
        {
            foreach (var key in appSettings.AllKeys)
            {
                o +=  string.Format("Key: {0} Value: {1}", key, appSettings[key]);
            }
        }
    }
    catch (ConfigurationErrorsException)
    {
        o = "AppSettings is empty.";
    }

    Label1.Text = o;
    Label2.Text = ConfigurationManager.ConnectionStrings["ConnStringDb2"].ConnectionString;
}

Result: enter image description here