基于服务总线触发器消息内容的 Azure Functions 动态输入/输出绑定

Azure Functions dynamic input / output binding based on Service Bus trigger message content

我正在尝试创建具有多个绑定的 Azure Functions 2.0。该函数由 Azure Service Bus Queue 消息触发,我想根据此消息内容读取 Blob。我已经尝试过以下代码:

public static class Functions
{
    [FunctionName(nameof(MyFunctionName))]
    public static async Task MyFunctionName(
        [ServiceBusTrigger(Consts.QueueName, Connection = Consts.ServiceBusConnection)] string message,
        [Blob("container/{message}-xyz.txt", FileAccess.Read, Connection = "StorageConnName")] string blobContent
    )
    {
        // processing the blob content
    }
}

但我收到以下错误:

Microsoft.Azure.WebJobs.Host: Error indexing method 'MyFunctionName'. Microsoft.Azure.WebJobs.Host: Unable to resolve binding parameter 'message'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).

我在某处看到可以使用动态绑定,但也许无法基于另一个输入绑定创建输入绑定。有什么想法吗?

我真的很惊讶这没有用。绑定有很多怪癖。请试一试:

public static class Functions
{
    [FunctionName(nameof(MyFunctionName))]
    public static async Task MyFunctionName(
        [ServiceBusTrigger(Consts.QueueName, Connection = Consts.ServiceBusConnection)] MyConcreteMessage message,
        [Blob("container/{message}-xyz.txt", FileAccess.Read, Connection = "StorageConnName")] string blobContent
    )
    {
        // processing the blob content
    }
}

创建 DTO:

public class MyConcreteMessage 
{
    public string message {get;set;}
}

确保您在服务总线中使用的消息是这样的:

{
  "message": "MyAwesomeFile"
}

它现在应该能够读取您的绑定 container/{message}-xyz.txt 并识别 message