通用错误处理程序生成错误消息重复项

时间:2018-12-17 16:54:56

标签: rebus

我在使用带有autofac的rebus的重复错误消息时遇到问题。 我试图在第二级重试中使用通用错误处理设置。设置如下:

错误处理程序:

public class ErrorHandler<T>: IHandleMessages<IFailed<T>>
{
    public ErrorHandler()
    {

    }

    public async Task Handle(IFailed<T> message)
    {

    }
}

注册:

var failedtyp = typeof(IHandleMessages<>).MakeGenericType(typeof(IFailed<>));
        builder.RegisterGeneric(typeof(ErrorHandler<>)).As(failedtyp).SingleInstance();

目的是对所有类型的消息使用相同的处理程序。 为特定类型的错误消息设置错误处理程序可以正常进行。

编辑:具有Rebus,Rebus.SqlServer和Rebus.Autofac的示例控制台应用程序

using System;
using System.Threading.Tasks;
using Autofac;
using Rebus.Bus;
using Rebus.Config;
using Rebus.Handlers;
using Rebus.Retry.Simple;

namespace RebusDuplicateTest
{
class Program
{
    private static IContainer container;

    static void Main(string[] args)
    {
        var cb = new ContainerBuilder();

        cb.RegisterRebus((conf,cc) => conf.Options(b => b
            .SimpleRetryStrategy(maxDeliveryAttempts:1,secondLevelRetriesEnabled: true))
            .Transport(c => c.UseSqlServer("Database=rebus_test_db;Server=(local);App=Hubert-Utv;MultipleActiveResultSets=True;Persist Security Info=True;Integrated Security=SSPI","queuename"))
            .Logging(c => c.None())
            .Options(o => o.SetMaxParallelism(1)));
        cb.RegisterHandler<Handler>();

        var failedtyp = typeof(IHandleMessages<>).MakeGenericType(typeof(IFailed<>));
        cb.RegisterGeneric(typeof(ErrorHandler<>)).As(failedtyp).SingleInstance();

        container = cb.Build();

        var bus = container.Resolve<IBus>();
        bus.SendLocal(new TestClass());

        Task.Delay(TimeSpan.FromMinutes(2)).Wait();
    }
}

public class Handler : IHandleMessages<TestClass>
{
    static int counter = 1;

    public async Task Handle(TestClass message)
    {
        Console.WriteLine($"Handler called {counter++} times");
        throw new Exception("Throws!");
    }
}

public class ErrorHandler<T> : IHandleMessages<IFailed<T>>
{
    private readonly IBus bus;

    private static int counter = 1;

    public ErrorHandler(IBus bus)
    {
        this.bus = bus;
    }

    public async Task Handle(IFailed<T> message)
    {
        Console.WriteLine($"Error handler called {counter++} times");
        await bus.DeferLocal(TimeSpan.FromSeconds(0),message.Message);
    }
}

public class TestClass
{
}
}

0 个答案:

没有答案