33

I am trying to configure some key/value pairs for my Azure web application using app settings section on Windows Azure preview portal.

enter image description here

Now I am trying to read values like below

ConfigurationManager.AppSettings["MyWebApp.DbConnectionString"];

but it returns null values.

Reading app settings from Web.config in my web application works fine.

Ashutosh B Bodake
  • 1,304
  • 1
  • 19
  • 31
  • 2
    What do you mean by `Reading app settings later in my web application works fine.`? Under what conditions you're getting this value as null? Please update your question with these details. – Gaurav Mantri Jun 14 '17 at 10:41
  • As a test, does it work if you use a simple name without periods? – David Ebbo Jun 14 '17 at 17:43

3 Answers3

43

I found the solution.

Keep values in web.config as well as in Azure App setting. When you are running/debugging application on your local environment it picks values from web.config.

When you deploy application on Azure it picks values from App setting.

//Below code work for both.
ConfigurationManager.AppSettings["KeyName"]

Keep key name same in web.config as well as in Azure app setting.

Ashutosh B Bodake
  • 1,304
  • 1
  • 19
  • 31
  • 1
    Just keep in mind that the value that is set in the portal takes precedence over the value in web.config – Byron Tardif Jun 15 '17 at 18:19
  • 7
    On local debug enviornment prioroty is for web.config and on deployment portal priority for Azure app setting. – Ashutosh B Bodake Jun 15 '17 at 18:37
  • 1
    guys, please mark this post as answer if it helps you. Thank you :) – Ashutosh B Bodake Apr 11 '18 at 11:55
  • 3
    Just in case anyone else spends ten minutes being a nitwit... remember there is a "Save" button at the top after you edit the strings on Azure. :) – Chris Rae Dec 21 '18 at 20:13
  • FYI this no longer works for core 2.2 at least. The value comes up blank. Instead the IConfiguration interface must be used as explained here: https://learn.microsoft.com/en-us/azure/app-service/containers/configure-language-dotnetcore#access-environment-variables – Kristaps Baumanis Nov 05 '19 at 20:22
  • @A X please try a solution in the below link. https://learn.microsoft.com/en-us/azure/app-service/configure-language-dotnetcore?pivots=platform-linux#access-environment-variables – Ashutosh B Bodake Jan 06 '21 at 15:12
21

In Azure, there are a few different ways of retrieving Application Settings and Connection Strings. However, connection strings work a little differently than vanilla application settings.

Application Settings can be retrieved by any method, regardless of whether or not they are present in the Web.config file.

Connection Strings can also be retrieved by any method if the string is defined in Web.config. However, if the connection string is NOT defined in Web.config, then it can only be retrieved using the Environment Variable method.

Retrieving as Environment Variable

Environment.GetEnvironmentVariable("APPSETTING_my-setting-key");
Environment.GetEnvironmentVariable("SQLAZURECONNSTR_my-connection-string-key");

Note that the keys must be prepended with a string designating their type when using this method.

All Application Settings use the APPSETTING_ prefix.

Connection Strings have a different prefix depending on the type of database selected when creating the string in the portal:

"Sql Databases" --> "SQLAZURECONNSTR_my-connection-string-key"
"SQL Server" --> "SQLCONNSTR_my-connection-string-key"
"MySQL" --> "MYSQLCONNSTR_my-connection-string-key"
"Custom" --> "CUSTOMCONNSTR_my-connection-string-key"

For a full overview, see the Windows Azure Web Sites documentation.

Will
  • 425
  • 4
  • 8
1
System.Environment.GetEnvironmentVariable("SERVICEBUS_CONNECTION")

works great!

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
  • Your post gives the impression of just saying "Thanks" for another users contribution. That risks being deleted for not being an answer. But I actually think that you contribute this. Consider rephrasing according to [answer]. Also... – Yunnosch Aug 09 '21 at 07:49
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Aug 09 '21 at 07:49