使用C#和Azure函数2.x将函数应用程序设置存储并加载为JSON对象

使用C#和Azure函数2.x将函数应用程序设置存储并加载为JSON对象,azure,azure-functions,Azure,Azure Functions,下面的应用程序设置按如下方式存储和读取: 已存储: GetEnvironmentVariable("Car_Id"); private static string GetEnvironmentVariable(string name) { return (string)System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)[name]; } local.sett

下面的应用程序设置按如下方式存储和读取:

已存储:

GetEnvironmentVariable("Car_Id");

private static string GetEnvironmentVariable(string name)
    {
        return (string)System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)[name];
    }
local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "Car_Id": "id",
    "Car_Name": "name"
  }
}
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
  Car: {  
   "Id": "id",
   "Name": "name"
  }
}

class Car{
  public int Id {get;set;}
  public string Name {get;set;}
}
已加载:

GetEnvironmentVariable("Car_Id");

private static string GetEnvironmentVariable(string name)
    {
        return (string)System.Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process)[name];
    }
是否可以将设置存储为对象并加载到对象中?

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "Car_Id": "id",
    "Car_Name": "name"
  }
}
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
  Car: {  
   "Id": "id",
   "Name": "name"
  }
}

class Car{
  public int Id {get;set;}
  public string Name {get;set;}
}
Visual studio 2017

Udpate

解决方案需要与Azure Function App设置上的设置兼容。也就是说,local.settngs.json上的设置能否保存在Azure Function app settings上

是否可以将设置存储为对象并加载到对象中

,您可以使用下面的代码来实现它

  public static async Task<IActionResult> Car(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Car")]
    HttpRequest httpRequest, ILogger log, ExecutionContext context)
  {
      log.LogInformation("C# HTTP trigger function processed a request.");

      var config = new ConfigurationBuilder()
          .SetBasePath(context.FunctionAppDirectory)
          .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
          .AddEnvironmentVariables()
          .Build();
      var cars= new Car();
      config.Bind("Car", cars);
      var b = cars.Id.ToString();
      log.LogInformation($"Car id is: {b}");
      return (ActionResult)new OkObjectResult($"Hello, {b}");
  }
汽车等级:

public class Car
{
    public int Id { get; set; }
    public string Name { get; set; }
}
快照:

有关更多详细信息,请参阅此


希望对您有所帮助:)

已回复链接