我在local.settings.json中有这些条目
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "whateverstorageaccountconnectionstring",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
},
"BusinessUnitMapping": {
"Values": {
"Connections": "CON",
"Products": "PRD",
"Credit & Affordability": "CAA",
"Accounts Receivable": "ARC",
"Identity": "IDT"
}
}
}
我有这段代码可以在启动时读取值
services.Configure<BusinessUnitMapping>(options => configuration.GetSection("BusinessUnitMapping").Bind(options));
其中BusinessUnitMapping为
public class BusinessUnitMapping
{
public Dictionary<string, string> Values { get; set; }
public BusinessUnitMapping()
{
Values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
}
当我在本地运行功能应用程序时,它可以毫无问题地将这些设置读入BusinessUnitMapping。
Azure门户中的应用程序设置的高级编辑仅允许如下所示的简单键值对
[
{
"name": "AzureWebJobsDashboard",
"value": "DefaultEndpointsProtocol=Somevalue",
"slotSetting": false
},
{
"name": "AzureWebJobsStorage",
"value": "DefaultEndpointsProtocol=Somevalue",
"slotSetting": false
},
...
]
-艾伦-
答案 0 :(得分:3)
- 这是将复杂的应用程序设置存储在Azure Function中的正确方法吗?
这仍然是一个悬而未决的问题:请参见this github issue asking exactly this
- 如何在Azure门户中为已部署的功能应用程序配置BusinessUnitMapping?
我当前的首选方法是将options pattern与使用GetEnvironmentVariable的委托一起使用,该委托将在本地和Azure中工作。缺点是您不能在本地设置文件本身中创建复杂的类型,但是您的对象可以任意复杂。
一个简单的例子:
在local.settings.json中:
{
...
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
...
"SomeSection:Setting1": "abc",
"SomeSection:Setting2": "xyz",
},
...
}
在您的创业公司:
services.Configure<MySettingsPoco>(o =>
{
o.Setting1 = Environment.GetEnvironmentVariable("SomeSection:Setting1");
o.Setting2 = Environment.GetEnvironmentVariable("SomeSection:Setting2");
});
然后在Azure中,您可以按照以下步骤创建这些设置:
答案 1 :(得分:1)
1。这是将复杂应用程序设置存储在Azure Function中的正确方法吗?
我们可以将这些条目保存在应用设置中。有关更多详细信息,请参阅document。详细步骤如下。
2。如何在Azure门户中为已部署的功能应用程序配置BusinessUnitMapping?
根据我的测试,我们可以使用以下代码来实现
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Collections.Generic;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var test =Environment.GetEnvironmentVariable("test",EnvironmentVariableTarget.Process);
log.LogInformation(test);
var values= JsonConvert.DeserializeObject<Dictionary<string, string>>(test);
var businessUnitMapping = new BusinessUnitMapping();
businessUnitMapping.Values = values;
log.LogInformation(businessUnitMapping.Values["Products"]);
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
public class BusinessUnitMapping
{
public Dictionary<string, string> Values { get; set; }
public BusinessUnitMapping()
{
}
}