c# - Azure Function 中的复杂对象应用设置

标签 c# azure .net-core azure-functions

我的 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
  },
  ...
]

问题

  1. 这是在 Azure Function 中存储复杂应用设置的正确方法吗?
  2. 如何在 Azure 门户中为我已部署的 Function App 配置 BusinessUnitMapping?

-艾伦-

最佳答案

  1. Is this a correct approach to store the complex app settings in Azure Function?

这仍然是一个悬而未决的问题:请参阅 this github issue asking exactly this

  1. How do I get BusinessUnitMapping configured in Azure Portal for the Function App that I have deployed?

我目前的首选方法是使用 options pattern使用使用 GetEnvironmentVariable 的委托(delegate),该委托(delegate)可以在本地和 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 中,您可以按如下方式创建这些设置:

enter image description here

关于c# - Azure Function 中的复杂对象应用设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57797225/

相关文章:

c# - 如何存储对基于传递给方法的表达式创建的泛型类型的引用?

c# - SQL查询结果到变量?

c# - 有没有办法部署WP8应用程序 "without Cable"

azure - 使用类似于 zipkin 的 azure 日志分析来跟踪 REST 跟踪时间的可能性

azure - 如何在逻辑应用 HTTP 触发器/操作上启用“即发即忘”/异步模式?

c# - ASP.NET Core 中必需的查询字符串参数

C#.NET,回发发生在事件处理程序执行之前?

asp.net-mvc - jQuery UI 图标在调试部署中显示,但在发布中不显示

.net - 如何在 Linux 上的 Visual Studio Code 中下载 dotnet 项目中的 nuget 依赖项源?

azure - 如何在 dotnet 应用程序中使用 Azure 应用服务的应用程序设置?