带有RabbitMQ简单事件的NServiceBus

iq0todco  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(153)

我希望能够使用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;
                }
            }
        }
    }
}
ibps3vxo

ibps3vxo1#

您的端点正在发布消息并接收消息。由于没有定义处理UserChanged消息(事件)的处理程序,因此NServiceBus可恢复性开始工作。您的选项包括
1.将端点声明为仅发送,以避免在未定义处理程序时处理消息
1.为UserChanged定义一个handler

相关问题