Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendOnly in NServiceBus

Tags:

c#

nservicebus

When creating a NServiceBus SendOnly endpoint, the purpose is to just fire-and-forget, i.e. just send a message and then someone else will take care of it. Which seems like the thing I need. I dont want any communication between the bus and the system handling messages. System "A" wants to notify system "B" about something.

Well the creation of an SendOnly endpoint if very straightforward but what about the system listening for messages from an SendOnly endpoint.

I'm trying to set up a listener in a commandline project that will handle messages. The messages get sent to the queue but they doesnt get handled by system "B".

Is this the wrong approach? Is a bus overkill for this type of functionality?

System A:

public class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();

        var bus = Configure.With()
            .UnityBuilder(container)
            .JsonSerializer()
            .Log4Net()
            .MsmqTransport()
            .UnicastBus()
            .SendOnly();

        while(true)
        {
            Console.WriteLine("Send a message");
            var message = new Message(Console.ReadLine());
            bus.Send(message);
        }
    }
}

System B:

class Program
{
    static void Main(string[] args)
    {
        var container = new UnityContainer();

        var bus = Configure.With()
            .UnityBuilder(container)
            .JsonSerializer()
            .Log4Net()
            .MsmqTransport()
            .UnicastBus()
            .LoadMessageHandlers()
            .CreateBus()
            .Start();

        Console.WriteLine("Waiting for messages...");

        while(true)
        {

        }
    }
}

public class MessageHandler : IHandleMessages<Message>
{
    public void Handle(Message message)
    {
        Console.WriteLine(message.Data);
    }
}

public class Message : IMessage
{
    public Message()
    {

    }

    public Message(string data)
    {
        Data = data;
    }

    public string Data { get; set; }
} 
like image 971
Peter Wikström Avatar asked Apr 17 '12 09:04

Peter Wikström


1 Answers

In the MessageEndpointMappings you need to update it as follows:

  1. Replace DLL with the name of the assembly containing your messages (e.g. "Messages")
  2. Change the Endpoint to the name of the queue which System B is reading from (You can check the queue name by looking in the MSMQ snapin under private queues).
<add Messages="Messages" Endpoint="SystemB" />

NServiceBus 3 automatically creates the queue name based upon the namespace of the hosting assembly.

Additionally, you may want to look at using the NServiceBus.Host to host your handlers instead of your own console application.

like image 173
Trevor Pilley Avatar answered Oct 12 '22 01:10

Trevor Pilley