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

从Azure函数中的local.settings.json读取自定义设置

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

    我正在尝试从local.settings.json文件检索自定义设置。示例我试图读取下面local.settings.json文件中的表列表

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
        "TableList": "TestTableName1,TestTableName2"
      }
    }
    

    string tableslist = ConfigurationManager.AppSettings["TableList"];
    

    它可以工作,但我在一些地方读到,这只在本地调试时工作,在生产环境中部署之后可能不工作。有人能指点我怎么做吗?或者该问题仅适用于与连接字符串相关的设置?

    1 回复  |  直到 7 年前
        1
  •  15
  •   Jerry Liu Phantom    7 年前

    @柯克和斯拉瓦帮你摆脱了困惑。只需添加一些细节供您参考。

    enter image description here

    关于如何在应用程序设置中获取参数,需要注意的一点是 ConfigurationManager

    1. 对于v1函数

      阅读Azure上的应用程序设置(同时 Values System.Environment.GetEnvironmentVariable($"{parameterName}")

      转到连接字符串,不幸的是GetEnvironmentVariable只在Azure上工作,因为连接字符串( ConnectionStrings 在local.settings.json中)不会导入到环境变量中。所以我们需要ConfigurationManager,它可以在Azure和本地env中工作。当然,它也可以读取应用程序设置。

    2. 对于v2函数,应用程序设置和连接字符串有两个选择。

      一种是使用GetEnvironmentVariable。我们可以参考 this list 用于Azure上连接字符串的前缀。

      // Get Application settings
      var appParameter= "AzureWebJobsStorage";
      System.Environment.GetEnvironmentVariable($"{appParameter}");
      
      // Get Connection strings(put local and Azure env together)
      var connParameter= "MySqlAzureConnection";
      var Prefix = "SQLAZURECONNSTR_";
      var connectionString = System.Environment.GetEnvironmentVariable($"ConnectionStrings:{connParameter}");
      if(string.IsNullOrEmpty(connectionString )){
         connectionString = System.Environment.GetEnvironmentVariable($"{Prefix}{connParameter}");
      }
      

      [FunctionName("FunctionName")]
      public static void Run(...,ExecutionContext context)
      {
         //"Values" and "Connection" sections are injected into EnvironmentVariables automatically hence we don't need to load Json file again. 
         //Hence SetBasePath and AddJsonFile are only necessary if you have some custom settings(e.g. nested Json rather than key-value pairs) outside those two sections. It's recommended to put those setting to another file if we need to publish them.
         //Note that Function binding settings(e.g. Storage Connection String) must be EnvironmentVariables, i.e. must be stored in "Values" section.
          var config = new ConfigurationBuilder()
              .SetBasePath(context.FunctionAppDirectory)
              .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
              .AddEnvironmentVariables()
              .Build();
      
          // Get Application Settings
          var appParameter= "AzureWebJobsStorage";
          string appsetting = config[$"{appParameter}"];
      
          // Get Connection strings
          var connParameter= "MySqlAzureConnection";
          string connectionString = config.GetConnectionString($"{connParameter}");
      }
      
        2
  •  0
  •   Joe Gurria Celimendiz    6 年前

    在.Net核心Azure函数中使用Environment.GetEnvironmentVariable时,我必须添加EnvironmentVariableTarget.Process参数来检索连接字符串

     string connectionString= Environment.GetEnvironmentVariable("DBConnectionString", 
     EnvironmentVariableTarget.Process);
    

     {"IsEncrypted": false,
     "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "DBConnectionString": "<conn string here>"
     }
    }