Rebus RabbitMq 无法订阅和检索项目

Rebus RabbitMq Unable to subscribe and retrieve item

我将对象添加到队列后无法检索它,添加它的代码如下,据我所知,它在队列中获取消息时工作正常。

var connection = "amqp://name:password@localhost";
using (var activator = new BuiltinHandlerActivator())
{
    Configure.With(activator)
        .Logging(l => l.ColoredConsole())
        .Transport(t => t.UseRabbitMqAsOneWayClient(connection).ExchangeNames(directExchangeName: "WamosExchange")) // .ExchangeNames(directExchangeName: "WamosExchange")
        .Routing(r => r.TypeBased()
            .Map<Wagon>("wagon_v1"))
        .Start();

    var wagon = new Wagon
    {
        Token = Guid.Parse("3CCE443C-249F-4FD2-9882-5830FB308B6B"),
        WagonId = Guid.Parse("A98A06AB-33B9-4A11-9DE2-DF0B8787B713"), 
        Description = "test", 
        WamosId = 12324, 
        YearBuilt = 1982
    };

    await activator.Bus.Send(wagon);

    Console.WriteLine("Done");
}

获取代码如下(这个不行)。我在使用 wagon_v1 队列的 RabbitMq 中使用交换。我看到 rebus 正在创建另一个以红色突出显示的。有点混乱。我希望它使用 WamosExchangewagon_v1 队列。

static BuiltinHandlerActivator activator = new BuiltinHandlerActivator();

static async Task MainSubscribeAsync()
{

    var connection = "amqp://name:password@localhost";

    activator.Register(() => new WagonHandler());

    var bus = Configure.With(activator)
        .Logging(l => l.ColoredConsole())
        .Transport(t => t.UseRabbitMq(connection, "wagon_v1")
            .ExchangeNames(directExchangeName: "WamosExchange"))
        .Routing(r => r.TypeBased()
            .Map<Wagon>("wagon_v1"))
        .Options(o =>
        {
            o.SetMaxParallelism(10);
            o.SetNumberOfWorkers(10);
        })
        .Start();

    await bus.Subscribe<Wagon>();

    Console.WriteLine("Done");
}

处理程序如下:

class WagonHandler : IHandleMessages
{
    public void Handle(Wagon message)
    {
        Console.WriteLine($"Token {message.Token}");
        Console.WriteLine($"WagonId {message.WagonId}");
    }
}

我在尝试订阅时遇到这个错误

Rebus.Retry.ErrorTracking.InMemErrorTracker WARN (Rebus 1 worker 4): Unhandled exception 1 while handling message with ID "f26daec4-48e2-47ca-a3fe-b4f18bc9b217"
Rebus.Retry.ErrorTracking.InMemErrorTracker WARN (Rebus 1 worker 2): Unhandled exception 2 while handling message with ID "f26daec4-48e2-47ca-a3fe-b4f18bc9b217"
Rebus.Retry.ErrorTracking.InMemErrorTracker WARN (Rebus 1 worker 8): Unhandled exception 3 while handling message with ID "f26daec4-48e2-47ca-a3fe-b4f18bc9b217"
Rebus.Retry.ErrorTracking.InMemErrorTracker WARN (Rebus 1 worker 7): Unhandled exception 4 while handling message with ID "f26daec4-48e2-47ca-a3fe-b4f18bc9b217"
Rebus.Retry.ErrorTracking.InMemErrorTracker WARN (Rebus 1 worker 6): Unhandled exception 5 while handling message with ID "f26daec4-48e2-47ca-a3fe-b4f18bc9b217"
Rebus.Retry.PoisonQueues.PoisonQueueErrorHandler ERROR (Rebus 1 worker 5): Moving message with ID "f26daec4-48e2-47ca-a3fe-b4f18bc9b217" to error queue "error"
System.AggregateException: 5 unhandled exceptions ---> Rebus.Exceptions.RebusApplicationException: Message with ID f26daec4-48e2-47ca-a3fe-b4f18bc9b217 and type ThreeSquared.VTGPAM.Objects.Wagon, ThreeSquared.VTGPAM.Objects could not be dispatched to any handlers
   at Rebus.Pipeline.Receive.DispatchIncomingMessageStep.<Process>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

如错误消息所述,找不到处理 Wagon 的消息处理程序。

你的经纪人

class WagonHandler : IHandleMessages
{
    public void Handle(Wagon message)
    {
        Console.WriteLine($"Token {message.Token}");
        Console.WriteLine($"WagonId {message.WagonId}");
    }
}

无法被 Rebus 找到,因为它寻找 IHandleMessages<Wagon> 的实现,而您只是使用了基本接口 IHandleMessages

实施 IHandleMessages<Wagon> 需要您稍微更改方法签名 – 它应该如下所示:

class WagonHandler : IHandleMessages<Wagon>
{
    public async Task Handle(Wagon message)
    {
        Console.WriteLine($"Token {message.Token}");
        Console.WriteLine($"WagonId {message.WagonId}");
    }
}