C# Azure功能:如何在运行时读取host.json中的设置?

C# Azure功能:如何在运行时读取host.json中的设置?,c#,azure,azure-functions,settings,C#,Azure,Azure Functions,Settings,有没有办法在运行时读取host.json文件中的主机设置? 假设您有这样一个主机文件: { “版本”:“2.0”, “扩展”:{ “服务总线”:{ “messageHandlerOptions”:{ “maxConcurrentCalls”:16 } } } } 然后如何从C代码中读取maxConcurrentCalls设置 如果默认值也包括在内,则更可取。您应该在启动时获得与控制台中打印的值相同的值: [28-01-2020 09:16:06] LoggerFilterOptions [28

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

{
“版本”:“2.0”,
“扩展”:{
“服务总线”:{
“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] }
[28-01-2020 09:16:06]日志过滤器选项
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]“最低水平”:“无”,
[28-01-2020 09:16:06]“规则”:[
[28-01-2020 09:16:06]     {
[28-01-2020 09:16:06]“ProviderName”:空,
[28-01-2020 09:16:06]“CategoryName”:空,
[28-01-2020 09:16:06]“日志级别”:空,
[28-01-2020 09:16:06]“过滤器”:“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”:空,
[28-01-2020 09:16:06]“日志级别”:“无”,
[28-01-2020 09:16:06]“过滤器”:空
[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”:空,
[28-01-2020 09:16:06]“日志级别”:空,
[28-01-2020 09:16:06]“过滤器”:“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]功能结果向导选项
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]“批量大小”:1000,
[28-01-2020 09:16:06]“刷新超时”:“00:00:30”,
[28-01-2020 09:16:06]“IsEnabled”:正确
[28-01-2020 09:16:06] }
[28-01-2020 09:16:06]单音选项
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]“锁定期”:“00:00:15”,
[28-01-2020 09:16:06]“ListenerLockPeriod”:“00:00:15”,
[28-01-2020 09:16:06]“锁获取超时”:“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]服务总线选项
[28-01-2020 09:16:06] {
[28-01-2020 09:16:06]“预取计数”:0,
[28-01-2020 09:16:06]“MessageHandlerOptions”:{
[28-01-2020 09:16:06]“自动完成”:正确,
[28-01-2020 09:16:06]“最大持续时间”:“00:05:00”,
[28-01-2020 09:16:06]“MaxConcurrentCalls”:192
[28-01-2020 09:16:06]   },
[28-01-2020 09:16:06]“会话句柄选项”:{
[28-01-2020 09:16:06]“自动完成”:正确,
[28-01-2020 09:16:06]“最大持续时间”:“00:05:00”,
[28-01-2020 09:16:06]“MaxConcurrentSessions”:2000,
[28-01-2020 09:16:06]“消息等待超时”:“00:01:00”
[28-01-2020 09:16:06]   },
[28-01-2020 09:16:06]“批量选项”:{
[28-01-2020 09:16:06]“MaxMessageCount”:1000,
[28-01-2020 09:16:06]“操作超时”:“00:01:00”,
[28-01-2020 09:16:06]“自动完成”:正确
[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”:错误,
[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] }
这就是你想要的吗

根据您的需求,我认为您可以设计一个函数来读取和解析json值。我想您知道host.json文件的格式是固定的,因此我可以在这里给出一个简单的示例:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

namespace HttpTrigger
{
    public static class Function1
    {
        public static string GetFileJson(string filepath)
        {
            string json = string.Empty;
            using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8")))
                {
                    json = sr.ReadToEnd().ToString();
                }
            }
            return json;
        }
        //Read Json Value
        public static string ReadJson()
        {
            string jsonfile = "host.json";
            string jsonText = GetFileJson(jsonfile);
            JObject jsonObj = JObject.Parse(jsonText);
            string value = ((JObject)jsonObj["extensions"])["serviceBus"]["messageHandlerOptions"]["maxConcurrentCalls"].ToString();
            return value;
        }
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string value = ReadJson();

            log.LogInformation("C# HTTP trigger function processed a request.");

            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" + value);
        }
    }
}
使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用系统文本;
命名空间HttpTrigger
{
公共静态类函数1
{
公共静态字符串GetFileJson(字符串文件路径)
{
string json=string.Empty;
使用(FileStream fs=newfilestream(filepath,FileMode.Open,System.IO.FileAccess.Read,FileShare.ReadWrite))
{
使用(StreamReader sr=newstreamreader(fs,Encoding.GetEncoding(“utf-8”))
{
json=sr.ReadToEnd().ToString();
}
}
返回json;
}
//读取Json值
公共静态字符串ReadJson()
{
字符串jsonfile=“host.json”;
字符串jsonText=GetFileJson(jsonfile);
JObject jsonObj=JObject.Parse(jsonText);
字符串值=((JObject)jsonObj[“扩展”])[“服务总线”][“messageHandlerOptions”][“maxConcurrentCalls”]。ToString();
返回值;
}
[功能名称(“功能1”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
字符串值=ReadJson();
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串名称=请求查询[“名称”];
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
名称=名称??数据?.name;
返回名称!=null
?(ActionResult)新的OkObjectResult($“你好,{name}”)
:new BadRequestObjectResult(“请在查询字符串或请求正文中传递名称”+值);
}
}
}
结果:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;

namespace HttpTrigger
{
    public static class Function1
    {
        public static string GetFileJson(string filepath)
        {
            string json = string.Empty;
            using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8")))
                {
                    json = sr.ReadToEnd().ToString();
                }
            }
            return json;
        }
        //Read Json Value
        public static string ReadJson()
        {
            string jsonfile = "host.json";
            string jsonText = GetFileJson(jsonfile);
            JObject jsonObj = JObject.Parse(jsonText);
            string value = ((JObject)jsonObj["extensions"])["serviceBus"]["messageHandlerOptions"]["maxConcurrentCalls"].ToString();
            return value;
        }
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string value = ReadJson();

            log.LogInformation("C# HTTP trigger function processed a request.");

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

您可以看到,我在host.json中获得了maxConcurrentCalls的值。Azure函数b上方的函数部分