NServiceBus 与 RabbitMQ 简单事件

发布于 2025-01-18 09:17:45 字数 2095 浏览 2 评论 0原文

我希望能够使用 NServiceBus 在 RabbitMQ 的队列中添加消息。我还不想处理它,所以只想查看队列中的一个项目,我的代码如下,但是当我运行它时出现此错误?

我一直在尝试查看文档,但似乎过于混乱。我熟悉 RabbitMq 并按原样使用它或与 Rabbit 客户端库一起使用它,但 NService 总线似乎使情况变得复杂和混乱!

输入图片此处描述

using Shared;
using System;
using System.Threading.Tasks;

namespace NServiceBus.RabbitMqTest
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var endpointConfiguration = new EndpointConfiguration("UserChanged");
            var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
            transport.UseConventionalRoutingTopology();
            transport.ConnectionString("host=localhost;username=guest;password=guest");

            //transport.Routing().RouteToEndpoint(typeof(MyCommand), "Samples.RabbitMQ.SimpleReceiver");
            endpointConfiguration.EnableInstallers();

            var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false);

            await SendMessages(endpointInstance);

            //await endpointInstance.Publish(new UserChanged { UserId = 76 });
            
            await endpointInstance.Stop().ConfigureAwait(false);
        }

        static async Task SendMessages(IMessageSession messageSession)
        {
            Console.WriteLine("Press [e] to publish an event. Press [Esc] to exit.");
            while (true)
            {
                var input = Console.ReadKey();
                Console.WriteLine();

                switch (input.Key)
                {
                    //case ConsoleKey.C:
                    //    await messageSession.Send(new MyCommand());
                    //    break;
                    case ConsoleKey.E:
                        await messageSession.Publish(new UserChanged { UserId = 87 });
                        break;
                    case ConsoleKey.Escape:
                        return;
                }
            }
        }
    }
}

I want to be able to used NServiceBus to add a message on a queue in RabbitMQ. I dont want to handle it yet so just want to see an item on the queue, my code is as follows, but I get this error when I run it?

I have been trying to look at the documentation but is seems overly confusing. I am familar with RabbitMq and using it as is or with the Rabbit client library, but NService bus seems to complicate and confuse the situation!

enter image description here

using Shared;
using System;
using System.Threading.Tasks;

namespace NServiceBus.RabbitMqTest
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var endpointConfiguration = new EndpointConfiguration("UserChanged");
            var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
            transport.UseConventionalRoutingTopology();
            transport.ConnectionString("host=localhost;username=guest;password=guest");

            //transport.Routing().RouteToEndpoint(typeof(MyCommand), "Samples.RabbitMQ.SimpleReceiver");
            endpointConfiguration.EnableInstallers();

            var endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false);

            await SendMessages(endpointInstance);

            //await endpointInstance.Publish(new UserChanged { UserId = 76 });
            
            await endpointInstance.Stop().ConfigureAwait(false);
        }

        static async Task SendMessages(IMessageSession messageSession)
        {
            Console.WriteLine("Press [e] to publish an event. Press [Esc] to exit.");
            while (true)
            {
                var input = Console.ReadKey();
                Console.WriteLine();

                switch (input.Key)
                {
                    //case ConsoleKey.C:
                    //    await messageSession.Send(new MyCommand());
                    //    break;
                    case ConsoleKey.E:
                        await messageSession.Publish(new UserChanged { UserId = 87 });
                        break;
                    case ConsoleKey.Escape:
                        return;
                }
            }
        }
    }
}

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

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

发布评论

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

评论(1

〆一缕阳光ご 2025-01-25 09:17:45

您的端点正在发布消息并接收消息。由于没有定义处理程序来处理 UserChanged 消息(事件),因此 NServiceBus 可恢复性开始发挥作用。您的选项是

  1. 将端点声明为 send-only 以避免在没有定义处理程序时处理消息
  2. 定义一个 UserChanged 的处理程序

Your endpoint is publishing the message as well as receiving it. Since there's no handler defined to handle the UserChanged messages (events), NServiceBus recoverability kicks in. Your options are

  1. Declare the endpoint as send-only to avoid handling the messages when there are no handlers defined
  2. Define a handler for UserChanged
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文