NServiceBus 与 RabbitMQ 简单事件

NServiceBus with RabbitMQ Simple event

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

时出现此错误

我一直在尝试查看文档,但似乎过于混乱。我熟悉 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;
                }
            }
        }
    }
}

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

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