从 C# 代码读取 Azure Function App 的函数超时

Read function timeout of Azure Function App from C# code

我在 host.json 部署时为我的 Azure Function App 设置了函数超时:

{
  "version": "2.0",
  "functionTimeout": "00:10:00", //max 10 minutes
   ...
}

但是我如何才能从我的 C# 函数代码中访问(读取)该值。这不是环境变量,是吗?

获取host.json变量的代码:

string jsonfile = "host.json";  
string jsonText = GetFileJson(jsonfile);  

JObject jsonObj = JObject.Parse(jsonText);  
string value = ((JObject)jsonObj)["functionTimeout"].ToString();

GetFileJson 方法:

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

添加的命名空间:

using Microsoft.Extensions.Logging;  
using Newtonsoft.Json.Linq;
using System.Text;
using System.IO;

输出:

我的函数Class文件:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;

namespace FunctionApp5
{
    public static class Function1
    {
        [Function("Function1")]
        public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req,
            FunctionContext executionContext)
        {
            var logger = executionContext.GetLogger("Function1");
            logger.LogInformation("C# HTTP trigger function processed a request.");

            logger.LogInformation(GetEnvironmentVariable("functionTimeout"));

            string jsonfile = "host.json";
            string jsonText = GetFileJson(jsonfile);

            JObject jsonObj = JObject.Parse(jsonText);
            string value = ((JObject)jsonObj)["functionTimeout"].ToString();

            var response = req.CreateResponse(HttpStatusCode.OK);
            response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

            response.WriteString("Welcome to Azure Functions!");

            return response;
        }
        public static string GetEnvironmentVariable(string name)
        {
            return name + ": " +
                System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
        }

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