如何从 ServiceBusAttribute 构造函数内部访问环境变量?

How to access the environment variables from inside ServiceBusAttribute constructor?

我有一个绑定到服务总线的 azure 函数,我是这样使用它的:

[FunctionName("MyFunctionName")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[ServiceBus("myqueueortopicname", Connection = "ServiceBusConnection")] IAsyncCollector<string> messages)

我正在尝试使服务总线队列名称可配置并取自 Environment.GetEnvironmentVariable("MyMessageBusQueueName")

[ServiceBus(Environment.GetEnvironmentVariable("MyMessageBusQueueName"), Connection = "ServiceBusConnection")] IAsyncCollector<string> messages)`

local.Settings.json:

{
    "IsEncrypted": false,
    "Values": {
        "MyMessageBusQueueName": "myqueueortopicname",
        "ServiceBusConnection": "..."
    },
    "ConnectionStrings": {}
}

但是我有一个错误:

An attribute argument must be a constant expression, typeof expression or array

您需要用 % 包裹环境变量名称来使用绑定表达式。将队列名称定义为 %MyMessageBusQueueName%,它将绑定到环境变量

[ServiceBus("%MyMessageBusQueueName%", Connection = "ServiceBusConnection")] IAsyncCollector<string> messages)

在 Microsoft 文档中阅读有关绑定表达式的信息 ---here