Home > Mobile >  Can i read environment variable from Azure App Settings by using Environment.GetEnvironmentVariable
Can i read environment variable from Azure App Settings by using Environment.GetEnvironmentVariable

Time:09-01

I am adding Firebase SDK to the server.
The first step is to set the GOOGLE_APPLICATION_CREDENTIALS environment variable,
so I set the environment variable in the enter image description here

CodePudding user response:

I am able to get the Azure Environment variables in my Environment. Please check the below workaround.

  • Created a .NET Framework App with MVC.
  • Installed the below NuGet Packages
Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration
Microsoft.Configuration.ConfigurationBuilders.Environment
System.Configuration.ConfigurationManager

Steps to Install NuGet Package

Right Click on the Solution Explorer => Manage NuGet Packages => Search in the Browse tab

enter image description here

  • In Web.config file => Under Configuration section add the below settings
 <configSections>
    <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
  </configSections>
  <configBuilders>
    <builders>
      <add name="DefaultEnvironment" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
    </builders>
  </configBuilders>
 
  <appSettings configBuilders="DefaultEnvironment">
    <add key="GOOGLE_APPLICATION_CREDENTIALS" value="Set via an environment variable - for example, dev, test, staging, or production connection string." />   
  </appSettings>
  • Add the New App setting in Azure Web App Configuration Section. enter image description here

  • Initially local web.config file contains the appsettings with some default value. Now I will override the value with Azure App Service Setting.

  • In HomeController, write the below code.

 public ActionResult Index()
        {
            var appsettings = ConfigurationManager.AppSettings;
            var local = appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
            var production = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
            ViewBag.GOOGLE_APPLICATION_CREDENTIALS = local;
            ViewBag.GOOGLE_APPLICATION_CREDENTIALS1 = production;
            return View();
        }
  • I am able to get the Environment variable by using both
var appsettings = ConfigurationManager.AppSettings;
appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
and 
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
  • In Home => Index.cshtml, add the below snippet somewhere in the file
 <h2> @ViewBag.GOOGLE_APPLICATION_CREDENTIALS - Local Settings</h2>
 <h2> @ViewBag.GOOGLE_APPLICATION_CREDENTIALS1 - Production Settings</h2>

Local App Output enter image description here

Published App OutPut : enter image description here

  • As you can see local settings are overridden by Azure App Settings.

References taken from MSDoc

  • Related