azure - 找不到主机.json

标签 azure azure-functions

我创建了一个新的azure函数,并且我有一个读取host.json的步骤,它在我的办公 table 上运行,但是当我发布到Azure时,我收到错误:

The configuration file 'host.json' was not found and is not optional. The physical path is 'D:\Program Files (x86)\SiteExtensions\Functions\2.0.12507\32bit\host.json'.

这是我尝试过的:

var Configuration = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("host.json",false)
                          .Build();

var value = Configuration["value"];

那么我该如何解决这个问题呢?

最佳答案

正如 Volodymyr 提到的,您需要在 Azure Function 方法中传递 ExecutionContext:。

public static async Task<IActionResult> Run( ... , ExecutionContext context) 
{
    ...
}

现在,当您构建配置时,您可以使用 ExecutionContext.FunctionAppDirectory 属性设置基本路径。我还可以选择添加 local.settings.json 以进行本地调试:

var config = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory) // Here you include the app directory from the context
    .AddJsonFile("host.json", optional: false, reloadOnChange: true) 
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) // for local debugging
    .AddEnvironmentVariables()
    .Build();
<小时/>

为了进一步改进您的代码,我建议为您的设置创建一个类。例如:

public sealed class FunctionSettings
{
    public string MySetting { get; set; } 
}

这样您就可以访问如下设置:

var settings = new FunctionSettings();
config.Bind(settings);

var value = settings.MySetting 

而不是

var value = Configuration["MySetting"];

关于azure - 找不到主机.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56625082/

相关文章:

azure - 延迟 azure pipeline YAML 中的作业

azure - Windows Azure VM负载平衡

Azure Function – 当前 Azure 资源名称

c# - Azure 函数代理响应正文不能包含大括号内的值

azure - 在 azure 函数中处理消息后,是否需要从服务总线队列中删除消息?

azure - Windows Azure 上长时间运行(或永远)的任务

c# - Azure Functions .NET Core 中的 EF Core 2.0 连接字符串

java - 如何在Spring Boot应用程序中设置变量instrumentationKey以进行a​​zure监控

azure - 如何在全局范围内扩展 Azure Functions 以实现无服务器?

asp.net-core - 通过 Azure Function 注入(inject) DbContext 时无法访问已处置的对象