我可以使用Environment.GetEnvironmentVariable方法从Azure应用程序设置中读取环境变量吗?

2023-11-29

我正在将 Firebase SDK 添加到服务器。
第一步是设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量,
所以我在中设置环境变量Azure 服务 -> 配置 -> 应用程序设置.
我检查了环境变量kudu too.
我尝试使用 Environment.GetEnvironmentVariable 方法从 ASP.NET 应用程序获取环境变量,
但它返回 null 并且应用程序设置的所有其他环境变量也返回 null。
我的项目是使用 .NET Framework 4.8 和 MVC5 构建的。
有这方面的信息吗?

using Microsoft.Ajax.Utilities;
using Microsoft.AspNet.Identity;
using Nest;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Net.Mail;
using System.ServiceModel.Channels;
using System.ServiceModel.Syndication;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace ---.Controllers
{
    public class HomeController : Controller
    {
        public async Task<ActionResult> Index(ActionParams param)
        {
            ...

            var appsettings = ConfigurationManager.AppSettings;
            var local = appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
            var production = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
            ViewBag.localEnv = local;
            ViewBag.productionEnv = production;

            return View("Home", "Index");
        }

This project is from my company, so i blocked some information and the language is korean.

enter image description here


我能够在我的环境中获取 Azure 环境变量。 请检查以下解决方法。

  • 使用 MVC 创建了一个 .NET Framework 应用程序。
  • 安装以下 NuGet 包
Microsoft.Configuration.ConfigurationBuilders.AzureAppConfiguration
Microsoft.Configuration.ConfigurationBuilders.Environment
System.Configuration.ConfigurationManager

安装 NuGet 包的步骤

右键单击解决方案资源管理器 => 管理 NuGet 包 => 在浏览选项卡中搜索

enter image description here

  • 在 Web.config 文件 => 在配置部分添加以下设置
 <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

  • 最初是本地的web.config文件包含appsettings有一些默认值。现在我将使用 Azure 应用服务设置覆盖该值。

  • In HomeController,编写以下代码。

 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();
        }
  • 我可以通过使用两者来获取环境变量
var appsettings = ConfigurationManager.AppSettings;
appsettings["GOOGLE_APPLICATION_CREDENTIALS"];
and 
Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
  • 在 Home => Index.cshtml 中,将以下代码片段添加到文件中的某处
 <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

  • 如您所见,本地设置被 Azure 应用程序设置覆盖。

参考文献取自MSDoc

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以使用Environment.GetEnvironmentVariable方法从Azure应用程序设置中读取环境变量吗? 的相关文章

随机推荐