代码之家  ›  专栏  ›  技术社区  ›  Pரதீப்

正在读取中的连接字符串local.settings.json在Azure中使用GetEnvironmentVariable

  •  1
  • Pரதீப்  · 技术社区  · 7 年前

    在Azure函数中 第1版 ,正在尝试读取中存在的连接字符串 local.settings.json GetEnvironmentVariable 静态法

    这是我的 文件

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
      },
      "ConnectionStrings": {
        "OnPremisesConnection": "Server=test;Initial Catalog=testdb;Integrated Security=SSPI;"
      }
    }
    

    使用以下代码读取连接字符串

    string variableName = "OnPremisesConnection";
    var res = Environment.GetEnvironmentVariable($"ConnectionStrings:{variableName}")
    

    但是我得到了 NULL 结果。我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Andrew Yanchak    6 年前

    好 啊。您可以尝试下一个:

    var config = new ConfigurationBuilder()
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();
     
     string connectionStr = config["ConnectionStrings:OnPremisesConnection"]
    
    推荐文章