如何从服务总线主题死信队列C#中读取?

时间:2019-08-19 10:22:47

标签: c# azure azureservicebus azure-servicebus-topics

下面的代码可以很好地读取服务总线队列死信,但对于主题,即使更改路径后,它抛出异常路径也不正确。

string path= "QueueName";

MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString("ConnecitonString");

MessageReceiver deadletterReceiver = await         messageFactory.CreateMessageReceiverAsync(QueueClient.FormatDeadLetterPath(path), ReceiveMode.PeekLock);

MessageSender sender = await messageFactory.CreateMessageSenderAsync(path);

BrokeredMessage deadLetter = await deadletterReceiver.ReceiveAsync(TimeSpan.Zero);
if (deadLetter != null)
{

现在我要对Service Bus Topic DeadLetter进行同样的操作 因此路径应为-topicName/subscriptionName

 string path= "TopicName/SubscriptionName";

    MessagingFactory messageFactory = MessagingFactory.CreateFromConnectionString("ConnecitonString");

    MessageReceiver deadletterReceiver = await messageFactory.CreateMessageReceiverAsync(TopicClient.FormatDeadLetterPath(path), ReceiveMode.PeekLock);

    MessageSender sender = await messageFactory.CreateMessageSenderAsync(path);

    BrokeredMessage deadLetter = await deadletterReceiver.ReceiveAsync(TimeSpan.Zero);
    if (deadLetter != null)
    {

获取异常路径不正确...

1 个答案:

答案 0 :(得分:1)

主题没有死队列,订阅则没有。您需要使用SubscriptionClient.FormatDeadLetterPath(String, String)方法,传递主题和订阅名称。