Using ServiceBusTrigger in the Webjobs SDK 3.x, can the Singleton attribute use a UserProperty as the scope?

坚强是说给别人听的谎言 提交于 2021-01-29 14:58:00

问题


I am using a ServiceBusTrigger to execute code when receiving a message. I would like to use the Singleton attribute to limit which messages can be executed in parallel. This attribute allows specifying a scope bound to properties on the incoming message, such that messages with different values can be executed in parallel but ones with the same value must be done serially.

This works when using top level properties on the incoming message object like CorrelationId.

Example

[Singleton("{CorrelationId}", SingletonScope.Function, Mode = SingletonMode.Function)]
public async Task HandleMessage(
    [ServiceBusTrigger("my-topic-name", "my-subscription-name"), ServiceBusAccount("my-account-name")]
    Message message,
    CancellationToken cancellationToken
)
{
    await Task.Yield();
}

What I am struggling to figure out is how to achieve the same behavior with user properties on the message. These are stored in the UserProperties dictionary on the Message object. I'm not seeing a way to refer to these with the binding statement in the Singleton attribute, but it seems like this would be a very common use case when combining Singleton with ServiceBusTrigger


回答1:


The Service Bus Bindings exposes Message Metadata in binding expressions. So, userProperties.<key> should do the trick.



来源:https://stackoverflow.com/questions/64732290/using-servicebustrigger-in-the-webjobs-sdk-3-x-can-the-singleton-attribute-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!