无法使用 Masstransit 读取队列消息

Impossible to read queue message with Masstransit

我有一个队列,其中有一些消息(使用 masstransit 创建)。

我尝试了这段代码来获取消息(见下文)。

我希望在 Console.Out 行收到消息,但我从未点击过这一行,消息仍在队列中。我没有收到任何错误。

有什么想法吗?

 class Program
    {
        static void Main(string[] args)
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(cfg =>
            {
    
                cfg.Host("localhost", "/", h =>
                {
                    h.Username("guest");
                    h.Password("guest");
                });
    
                cfg.ReceiveEndpoint("myQueue", e =>
                {
                    e.Handler<ProcessingQueue>(context =>
                    {
                        return Console.Out.WriteLineAsync($"{context.Message.Id}");
                    });
    
                });
    
            });
        }
    }
    
    public class ProcessingQueue
    {
        public int Id { get; set; }
    
        public string Name { get; set; }
    }

谢谢,

我尝试添加:

bus.Start();
Console.WriteLine("Receive listening for messages");
Console.ReadLine();
bus.Stop();

但是当我这样做时,会创建一个新队列 myQueue_skipped 是用我的消息创建的。

如果消息被移动到 _skipped 队列,则表示这些消息未被该接收端点上配置的任何消费者使用。最常见的错误 as highlighted at the top of the message documentation 是不匹配的命名空间。

相似答案:here

尝试使用此代码作为 ReceiveEndpoint

cfg.ReceiveEndpoint("myQueue", e =>
{
    e.Consumer<MessagesConsumer>();
});

“MessagesConsumer”必须继承自 IConsumer

public class MessagesConsumer: IConsumer<ProcessingQueue>      
{   public async Task Consume(ConsumeContext<ProcessingQueue> context)
        {
             //access to the properties
             var name=context.Message.Name;
             var id=context.Message.Id;
        }
}

在Consume 方法中,您将收到“ProcessingQueue”类型的消息。您可以在此处访问属性..