问题
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