c# - Azure Functions : how do you read the settings in host. json 在运行时?

标签 c# azure azure-functions settings

有没有办法在运行时读取host.json文件中的主机设置? 假设您有一个如下所示的主机文件:

{
  "version": "2.0",
  "extensions": {
    "serviceBus": {
      "messageHandlerOptions": {
        "maxConcurrentCalls": 16
      }
    }
  }
}

如何从 C# 代码中读取 maxConcurrentCalls 设置?

如果也包含默认值那就更好了。您应该获得与启动时在控制台中打印的相同的值:

[28-01-2020 09:16:06] LoggerFilterOptions
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]   "MinLevel": "None",
[28-01-2020 09:16:06]   "Rules": [
[28-01-2020 09:16:06]     {
[28-01-2020 09:16:06]       "ProviderName": null,
[28-01-2020 09:16:06]       "CategoryName": null,
[28-01-2020 09:16:06]       "LogLevel": null,
[28-01-2020 09:16:06]       "Filter": "<AddFilter>b__0"
[28-01-2020 09:16:06]     },
[28-01-2020 09:16:06]     {
[28-01-2020 09:16:06]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[28-01-2020 09:16:06]       "CategoryName": null,
[28-01-2020 09:16:06]       "LogLevel": "None",
[28-01-2020 09:16:06]       "Filter": null
[28-01-2020 09:16:06]     },
[28-01-2020 09:16:06]     {
[28-01-2020 09:16:06]       "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
[28-01-2020 09:16:06]       "CategoryName": null,
[28-01-2020 09:16:06]       "LogLevel": null,
[28-01-2020 09:16:06]       "Filter": "<AddFilter>b__0"
[28-01-2020 09:16:06]     }
[28-01-2020 09:16:06]   ]
[28-01-2020 09:16:06] }
[28-01-2020 09:16:06] FunctionResultAggregatorOptions
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]   "BatchSize": 1000,
[28-01-2020 09:16:06]   "FlushTimeout": "00:00:30",
[28-01-2020 09:16:06]   "IsEnabled": true
[28-01-2020 09:16:06] }
[28-01-2020 09:16:06] SingletonOptions
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]   "LockPeriod": "00:00:15",
[28-01-2020 09:16:06]   "ListenerLockPeriod": "00:00:15",
[28-01-2020 09:16:06]   "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
[28-01-2020 09:16:06]   "LockAcquisitionPollingInterval": "00:00:05",
[28-01-2020 09:16:06]   "ListenerLockRecoveryPollingInterval": "00:01:00"
[28-01-2020 09:16:06] }
[28-01-2020 09:16:06] ServiceBusOptions
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]   "PrefetchCount": 0,
[28-01-2020 09:16:06]   "MessageHandlerOptions": {
[28-01-2020 09:16:06]     "AutoComplete": true,
[28-01-2020 09:16:06]     "MaxAutoRenewDuration": "00:05:00",
[28-01-2020 09:16:06]     "MaxConcurrentCalls": 192
[28-01-2020 09:16:06]   },
[28-01-2020 09:16:06]   "SessionHandlerOptions": {
[28-01-2020 09:16:06]     "AutoComplete": true,
[28-01-2020 09:16:06]     "MaxAutoRenewDuration": "00:05:00",
[28-01-2020 09:16:06]     "MaxConcurrentSessions": 2000,
[28-01-2020 09:16:06]     "MessageWaitTimeout": "00:01:00"
[28-01-2020 09:16:06]   },
[28-01-2020 09:16:06]   "BatchOptions": {
[28-01-2020 09:16:06]     "MaxMessageCount": 1000,
[28-01-2020 09:16:06]     "OperationTimeout": "00:01:00",
[28-01-2020 09:16:06]     "AutoComplete": true
[28-01-2020 09:16:06]   }
[28-01-2020 09:16:06] }
[28-01-2020 09:16:06] HttpOptions
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]   "DynamicThrottlesEnabled": false,
[28-01-2020 09:16:06]   "MaxConcurrentRequests": -1,
[28-01-2020 09:16:06]   "MaxOutstandingRequests": -1,
[28-01-2020 09:16:06]   "RoutePrefix": "api"
[28-01-2020 09:16:06] }

最佳答案

这对我有用:

public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        var config = builder.GetContext().Configuration;
        string maxConcurrentCalls = config.GetSection("AzureFunctionsJobHost:extensions:serviceBus:messageHandlerOptions:maxConcurrentCalls").Value;
    }
}

关于c# - Azure Functions : how do you read the settings in host. json 在运行时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59945425/

相关文章:

C# Switch-case 字符串开头

C#4 : Dynamic and Nullable<>

c# - 在azure sdk Fluent中使用身份验证 token

c# - Microsoft.WindowsAzure.ServiceRuntime 的 NuGet 包在哪里?

c# - 使用 Azure 事件中心和 Azure 函数进行管道处理

azure - 使用 Azure Functions 通过 Amazon SES 发送电子邮件

c# - 根据用户角色呈现不同的 View ASP.NET MVC 5

c# - TextBox - asp.net 上是否有进入和离开事件?

python - pyodbc.Error : Data source name not found, 且未指定默认驱动程序 (0) (SQLDriverConnect)')

azure-eventhub - 在 Azure Functions 中创建 Azure EventHub 消息