如何以编程方式重新发送EasyNetQ出错的消息?

时间:2019-05-14 10:25:54

标签: c# .net-core rabbitmq easynetq

我想知道如何在不使用HosePipe的情况下以编程方式重新发送EasyNetQ出错的消息,即使用其原始发送交换将其重新发送到其原始目标队列。

有可能吗?

1 个答案:

答案 0 :(得分:0)

我想出的解决方案是:

public static class AdvancedBusExtensions
{
    public static async Task ResendErrorsAsync(this IAdvancedBus source, string errorQueueName)
    {
        var errorQueue = await source.QueueDeclareAsync(errorQueueName);
        var message = await source.GetMessageAsync(errorQueue);
        while (message != null)
        {
            var utf8Body = Encoding.UTF8.GetString(message.Body);
            var error = JsonConvert.DeserializeObject<Error>(utf8Body);
            var errorBodyBytes = Encoding.UTF8.GetBytes(error.Message);
            var exchange = await source.ExchangeDeclareAsync(error.Exchange, x =>
            {
                // This can be adjusted to fit the exchange actual configuration 
                x.AsDurable(true);
                x.AsAutoDelete(false);
                x.WithType("topic");
            });
            await source.PublishAsync(exchange, error.RoutingKey, true, error.BasicProperties, errorBodyBytes);
            message = await source.GetMessageAsync(errorQueue);
        }
    }
}