如果发生异常,有没有办法使用 C# 将消息发送到死信主题?
If exception occurs is there any way to send message into dead letter topic using c#?
我有 azure 函数应用程序,它根据进入服务总线主题的新消息触发。 maxdeliverycount=10
但是,如果发生任何异常,我想将消息发送到死信主题中,该怎么做?
[FunctionName("ProcessData")]
public static void Run([ServiceBusTrigger("topic", "sub1", Connection = "ConnectionStringSettingName")]string mySbMsg, TraceWriter log)
{
try
{
//process message mySbMsg
DataSchema data = JsonConvert.DeserializeObject<DataSchema>(mySbMsg);
...
}
catch(Exception ex)
{
// How to send message into Dead letter explicitly ?
}
}
以下代码片段显示了 MessageReceiver 用法的示例:
using Microsoft.Azure.ServiceBus.Core;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
public static class Function8
{
[FunctionName("Function8")]
public static async Task Run([ServiceBusTrigger("topic2", "test2", Connection = "AzureServiceBusConnectionString")]
string mySbMsg, string lockToken, MessageReceiver messageReceiver, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
try
{
throw new Exception();
// await messageReceiver.CompleteAsync(lockToken);
}
catch (Exception ex)
{
await messageReceiver.DeadLetterAsync(lockToken);
}
}
}
我有 azure 函数应用程序,它根据进入服务总线主题的新消息触发。 maxdeliverycount=10
但是,如果发生任何异常,我想将消息发送到死信主题中,该怎么做?
[FunctionName("ProcessData")]
public static void Run([ServiceBusTrigger("topic", "sub1", Connection = "ConnectionStringSettingName")]string mySbMsg, TraceWriter log)
{
try
{
//process message mySbMsg
DataSchema data = JsonConvert.DeserializeObject<DataSchema>(mySbMsg);
...
}
catch(Exception ex)
{
// How to send message into Dead letter explicitly ?
}
}
以下代码片段显示了 MessageReceiver 用法的示例:
using Microsoft.Azure.ServiceBus.Core;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
public static class Function8
{
[FunctionName("Function8")]
public static async Task Run([ServiceBusTrigger("topic2", "test2", Connection = "AzureServiceBusConnectionString")]
string mySbMsg, string lockToken, MessageReceiver messageReceiver, ILogger log)
{
log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
try
{
throw new Exception();
// await messageReceiver.CompleteAsync(lockToken);
}
catch (Exception ex)
{
await messageReceiver.DeadLetterAsync(lockToken);
}
}
}