gpt4 book ai didi

c# - Azure函数读取appsetting.json

转载 作者:行者123 更新时间:2023-12-03 05:18:35 25 4
gpt4 key购买 nike

Azure 函数不会读取 appsetting.json 中的配置,而是从 local.settings.json 读取

启动

    public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{


}

public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
var context = builder.GetContext();

builder.ConfigurationBuilder
.AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
.AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false)
.AddEnvironmentVariables();
}


}

触发器

        [FunctionName("func")]
public async Task Run([ServiceBusTrigger("topicName", "subName", Connection = "ConnectionString")] EmployeeMessageModel employeeMessage)
{


}

错误:服务总线帐户连接字符串“ConnectionString”不存在。确保它是已定义的应用程序设置。

应用程序设置.json

{"ConnectionString": "ConnectionString"}

如果我将其添加到 local.settings.json 它会找到它。知道我错过了什么吗?

最佳答案

在门户中,如果要添加连接字符串,则需要在“配置”选项卡的“应用程序设置”中添加,如下所示:

enter image description here

enter image description here

当您发布到门户时,您需要执行类似的操作。

如果您想在本地连接服务总线,您需要在 Local.settings.json 中添加服务总线连接字符串,如下所示:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"Connection": "Connection string here"
}
}

enter image description here

在应用程序设置中仅支持环境变量(键)而不支持连接字符串,仅在本地settings.json中支持连接字符串因此您需要使用本地设置。

关于c# - Azure函数读取appsetting.json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73822115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com