简体   繁体   中英

Can i read environment variable from Azure App Settings by using Environment.GetEnvironmentVariable method?

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 Azure Service -> Configuration -> App Setting .
I checked environment variables from kudu too.
And I tried to get an environmental variable from my ASP.NET app using Environment.GetEnvironmentVariable method,
but It returns null and all the other environmental variables of the app settings return null too.
My project is built with .NET Framework 4.8 and MVC5.
Is there any information about this?

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

在此处输入图像描述

  • 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.在此处输入图像描述

  • 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在此处输入图像描述

Published App OutPut:在此处输入图像描述

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

References taken from MSDoc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM