使用 C# 库创建 RabbitMQ 服务器时出现异常

发布于 2024-10-24 19:46:48 字数 881 浏览 6 评论 0原文

我有一个控制台应用程序,它启动 RabbitMQ 服务器(作为使用rabbitmq-server.bat 文件的应用程序),然后尝试在其顶部创建一个队列并开始侦听消息。代理启动正常,但是一旦我尝试声明队列,我就会收到一个异常,指出队列名称不存在。我对此有点困惑,因为我正在尝试创建队列,并且不知道为什么它要寻找具有该名称的现有队列。

这是我用来运行服务器的代码:

var model = QueueModelFactory.CreateModel();
model.ExchangeDeclare(exchangeName, ExchangeType.Fanout, true);
model.QueueDeclare(QueueName, false, false, false, null);
model.QueueBind(QueueName, exchangeName, "");

var subscription = new Subscription(model, QueueName, false);

while (true)
{
    var args = subscription.Next();
    ProcessQueueItem(args.Body);
    subscription.Ack(args);
}

异常发生在调用 QueueDeclare 的行上。我得到的确切异常是:

“AMQP操作被中断:AMQP关闭原因,由Peer发起,code = 404,text =“NOT_FOUND - vhost '/'中没有队列'FavorCompletions'”,classId = 50,methodId =10,原因=”。

我曾经一度让这个工作正常,然后重构了一些代码却让它崩溃了。我不知道我做错了什么,因为所有示例应用程序似乎都做了完全相同的事情。

任何帮助将不胜感激。

I've got a console application that starts up the RabbitMQ server (as an app using the rabbitmq-server.bat file) and then attempts to create a queue on top of it and begin listening for messages. The broker starts up fine but once I try to declare the queue I get an exception stating the queue name doesn't exist. I'm a bit confused at this since I'm trying to create the queue, and don't know why it is looking for an existing one with that name.

Here's the code I'm using to run the server:

var model = QueueModelFactory.CreateModel();
model.ExchangeDeclare(exchangeName, ExchangeType.Fanout, true);
model.QueueDeclare(QueueName, false, false, false, null);
model.QueueBind(QueueName, exchangeName, "");

var subscription = new Subscription(model, QueueName, false);

while (true)
{
    var args = subscription.Next();
    ProcessQueueItem(args.Body);
    subscription.Ack(args);
}

The exception happens on the line the calls QueueDeclare. The exact exception that I get is:

"The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=404, text="NOT_FOUND - no queue 'FavorCompletions' in vhost '/'", classId=50, methodId=10, cause=".

I had this working at one point and then refactored some code only to have it break. I have no clue what I'm doing wrong, since all the sample apps seem to do the exact same thing.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘落散花 2024-10-31 19:46:48

不知道 CreateModel()QueueModelFactory 代码中有什么,并查看使用 ExchangeDeclare、QueueDeclare 和 QueueBind 的其余代码,它们看起来很好。

我只能建议您如何创建连接/模型存在问题。

将您的替换

var model = QueueModelFactory.CreateModel();

为 :

IModel model = new ConnectionFactory { Address = "127.0.0.1" }
    .CreateConnection()
    .CreateModel();

似乎会启动并且不会导致该异常。

我以前经历过您的异常,但没有缩小其确切原因,在我的一个案例中,我在声明交换 model.ExchangeDeclare() 部分时犯了一个错误,所以这是一个需要解决的问题也看看。

Not knowing what is in your QueueModelFactory code for CreateModel() and looking at the rest of the code the use of ExchangeDeclare, QueueDeclare and QueueBind they seem fine.

All I can suggest is there's an issue with how you go about creating a connection / model.

Substituting your

var model = QueueModelFactory.CreateModel();

with :

IModel model = new ConnectionFactory { Address = "127.0.0.1" }
    .CreateConnection()
    .CreateModel();

Seems to kick off and not cause that exception.

I have experienced your exception before, but haven't narrowed the exact cause of it, in one of my cases I made a mistake in declaring the exchange model.ExchangeDeclare() portion, so that's an area to look at too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文