c# - ServiceBusTrigger 选择错误的配置文件

标签 c# azure azure-functions azureservicebus

我的 Azure 函数项目中有 4 个配置文件,代表 4 个环境。

My configs

我有以下代码来根据 ASPNETCORE_ENVIRONMENT 红色特定于环境的文件

        var executionContextOptions = builder.Services.BuildServiceProvider()
        .GetService<IOptions<ExecutionContextOptions>>().Value;

        var currentDirectory = executionContextOptions.AppDirectory;

        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        var config = new ConfigurationBuilder()
            .SetBasePath(currentDirectory)
            .AddJsonFile($"local.settings.{environment}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
        .Build();
         builder.Services.AddSingleton<IConfiguration>(config);

我可以通过 DI 访问 key ,例如 _configuration.GetSection("Values") 我现在有一个 ServiceBusTrigger

[FunctionName("FuncEmployeeMessageActivity")]
    public static void FuncEmployeeMessageActivity(
    [ServiceBusTrigger("queue-xxxxx", Connection = "ServiceBus")] string message,
    ILogger log)
    {
        try
        {
            // Process the message received from the Service Bus queue.
            log.LogInformation($"Received message: {message}");
        }
        catch (Exception)
        {
            throw;
        }
    }

此处,Connection 属性正在寻找 local.settings.json 中的键 ServiceBus,而不是 local.dev.settings .json 因为我在 VS 中将 ASPNETCORE_ENVIRONMENT 设置为 dev 。我该如何解决这个问题?

Warning: Cannot find value named 'ServiceBus' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger'

最佳答案

目前你不能。具有连接字符串的触发器(例如 Azure 服务总线)需要在开发期间在 local.settings.json 中找到连接字符串,并在部署时通过设置将其设置为环境变量。

This thread适用于新的 SDK,但仍然与您的问题相关,并附有详细信息。

关于c# - ServiceBusTrigger 选择错误的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77215511/

相关文章:

c# - configuration.getValue 或 configuration.getsection 始终返回 null

c# - 如何在.NET Compact Framework 2.0 中播放感叹号或星号音?

azure - 通过 Azure DevOps Pipelines 部署 DACPAC 时出错

Azure函数无法连接到Azure SQL数据库

python - Azure Functions - 获取每个请求中使用的出站 IP 地址

c# - 通过接口(interface)访问对象

azure - 如何提供从公共(public)互联网到 Azure 私有(private) kubernetes 集群中托管的应用程序的入站访问

azure - 如何调试 B2C 权限不足的 Azure 应用程序?

azure - 在 Azure 中安排长时间运行的任务

c# - C# 中 TryParse() 的通用包装器