服务总线的 Azure 函数绑定

Azure Function Bindings for Service Bus

我正在关注 this documentation 为服务总线队列创建触发器。

我希望能够访问消息属性。我以为我可以像这样简单地将 Dictionary<string, object> properties 添加到参数列表中:

public static void Run(
        [ServiceBusTrigger(QueueName, Connection = "connectionSetting")]
        // Message message,
        string myQueueItem,
        Int32 deliveryCount,
        DateTime enqueuedTimeUtc,
        string messageId,
        string ContentType,
        Dictionary<string,object> properties,
        TraceWriter log)

但是抛出:

Error indexing method 'Program.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'properties' to type Dictionary`2. Make sure the parameter Type is supported by the binding.

这是 a list 可能的参数绑定。我哪里错了?

更新:

我尝试将签名更改为

public static void Run(
        [ServiceBusTrigger(QueueName, Connection = "connectionSetting")]
        // Message message,
        string myQueueItem,
        Int32 deliveryCount,
        DateTime enqueuedTimeUtc,
        string messageId,
        string ContentType,
        IDictionary<string, object> properties,
        TraceWriter log)

它产生同样的错误:

Error indexing method 'Program.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'properties' to type IDictionary

IDictionary<string, object> properties

更新:

对于版本 2,使用以下绑定:

public static void Run(
        [ServiceBusTrigger(QueueName, Connection = "connectionSetting")]
        Message message,
        string label,
        Int32 deliveryCount,       
        DateTime enqueuedTimeUtc,  
        string messageId,
        string ContentType, 
        ILogger log)
    {           
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {Encoding.UTF8.GetString(message.Body)}");
        var userProperties = message.UserProperties;
    }

对于函数 v2 运行时,参数名称已更改为 UserProperties

要修复错误,请将参数更新为以下内容:

IDictionary<string, object> UserProperties

这是服务总线扩展的相关代码。

https://github.com/Azure/azure-webjobs-sdk/blob/42a711763ddecca9df4caae9c7dc5fe16178880c/src/Microsoft.Azure.WebJobs.ServiceBus/Triggers/ServiceBusTriggerBinding.cs#L127