英文:
How to redirect message to dead-letter queue Azure Service Bus
问题
我正在使用隔离的 Azure 函数来接收队列中的消息。我需要验证接收到的消息,如果无效,将其发送到死信队列。我找到的唯一方法是引发异常,经过 10 次重试后,消息将被移动到死信队列。当然,这不是一个好的解决方案。也许有人遇到过同样的任务?谢谢!
[Function("Example")]
public async Task ExampleAsync([ServiceBusTrigger("example", Connection = "ServiceBusConnection")] string entityId)
{
if (!int.TryParse(entityId, out var id))
{
// TODO: 移动到死信队列
}
}
英文:
I'm using isolated azure function to receive message from the queue. I need to validate received message and if it invalid, send it to the dead-letter queue. The only way how to do it I found is to throw exception and after 10 retries the message will be moved to dead letter queue. Of course it is not good solution. Maybe anyone faced the same task? Thanks!
[Function("Example")]
public async Task ExampleAsync([ServiceBusTrigger("example", Connection = "ServiceBusConnection")] string entityId)
{
if (!int.TryParse(entityId, out var id))
{
// TODO: Move to dead-letter queue
}
}
答案1
得分: 2
如果您正在使用内部处理工作程序,可以通过将绑定到函数中的 ServiceBusMessageActions
并调用其 DeadLetterMessageAsync
方法来将消息移至死信。 这还要求您绑定到 ServiceBusMessage
而不仅仅是 string entityId
。
例如:
[FunctionName("BindingToMessageActions")]
public static async Task Run(
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
ServiceBusReceivedMessage message,
ServiceBusMessageActions messageActions)
{
if (!int.TryParse(message.Body.ToString(), out var id))
{
await messageActions.DeadLetterMessageAsync(message);
}
}
更多示例可在包的 README 中找到。
遗憾的是,在隔离的处理模型中暂时还不可用。
英文:
If you're using the in-process worker, you can dead-letter by binding to ServiceBusMessageActions
in your function and then calling its DeadLetterMessageAsync
method. This would also require that you bind to ServiceBusMessage
rather than just string entityId
.
For example:
[FunctionName("BindingToMessageActions")]
public static async Task Run(
[ServiceBusTrigger("<queue_name>", Connection = "<connection_name>")]
ServiceBusReceivedMessage message,
ServiceBusMessageActions messageActions)
{
if (!int.TryParse(message.Body.ToString()), out var id))
{
await messageActions.DeadLetterMessageAsync(message);
}
}
More examples can be found in the package README.
Unfortunately, this is not yet available in the isolated process model.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论