如何解决无法使用 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”类型的消息。您可以在此处访问属性..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。