0

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;
                }
            }
        }
    }
}
Andrew
  • 2,571
  • 2
  • 31
  • 56

1 Answers1

3

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
Sean Feldman
  • 23,443
  • 7
  • 55
  • 80