c# - Azure函数读取appsetting.json

标签 c# azure function azure-functions azureservicebus

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/

相关文章:

c# - 添加此项目作为引用会导致循环依赖 mvvm

azure - 将富文本编辑器结果上传到 windows azure

visual-studio-2010 - 为什么我的 Azure 部署仍然有远程桌面的第三个终结点,即使我已禁用它?

c++ - std::tr1::function - 在公共(public)容器中存储各种类型的函数对象

c - 通过函数传递变量 C

android - android中startActivity()之后如何调用onCreate()?它之间的具体流程是什么?

c# - LinQ to SQL 在使用 Any() 时抛出 Stackoverflow 异常

c# - 解析 XPath 表达式

c# - 从 C# 在 .lib 文件中创建 C++ 类的实例

azure - 当我们收到服务器错误时如何添加 azure 警报