gpt4 book ai didi

azure - 使用 C# 和 Azure Functions 2.x 将函数应用设置存储和加载为 JSON 对象

转载 作者:行者123 更新时间:2023-12-03 02:52:45 24 4
gpt4 key购买 nike

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

已存储:

本地.settings.json

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

已加载:

GetEnvironmentVariable("Car_Id");

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

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

本地.settings.json

{
"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

更新

该解决方案需要与 Azure Function App 设置上的设置兼容。也就是说,local.settngs.json 上的设置可以保存在 Azure Function 应用设置中吗?

最佳答案

Is it possible to store the settings as object and load it into an object?

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

  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}");
}

local.settings.json如下:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxxx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"Car": {
"Id": "123456",
"Name": "name_1"
}
}

汽车类:

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

快照:

enter image description here

更多详情,您可以引用这个article

希望对你有帮助:)

关于azure - 使用 C# 和 Azure Functions 2.x 将函数应用设置存储和加载为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55053325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com