1
votes

I am new to the NServiceBus world. I have created a new NServiceBus Project with a Send/Receive NServiceBus "client", a command which sends a message to the console app, and a send/receive selfhost (console application).

I run the service bus "client" via NServiceBus.Host.exe. I can submit messages via the following code:

public class OrderMessaging
{
    public IBus Bus { get; set; }

    public void SendRouteMessageReceived(LabRoutingUpdateMessage routingUpdateMessage)
    {
        Bus.Send(new RouteMessageReceived(routingUpdateMessage));
    }
}

I send a test message from another cs file within the client bus:

class OrderManagementTest : IWantToRunAtStartup
{
    public IBus _bus { get; set; }

    public void Run()
    {
        Console.WriteLine("Press 'Enter' to send a message.To exit, Ctrl + C");

        while (Console.ReadLine() != null)
        {
            Int64 _trackNum = 999245;

            Console.WriteLine("Track Number: {0}", _trackNum.ToString());

            _labRouteMessage = new LabRoutingUpdateMessage(_trackNum);

            OrderMessaging Message = new OrderMessaging();
            Message.Bus = _bus;
            Message.SendRouteMessageReceived(_labRouteMessage);
        }
    }
}

My selfhost bus initilization looks like this:

public static class Startup 
{
    static void Main(string[] args)
    {
        // Setup the bus
        BusInitializer.Init();
        Console.Clear();
        Console.ReadLine();  //need to handle messages here from the client 
    }
}

public class BusInitializer 
{
    public static IBus Bus { get; private set; }

    public static void Init()
    {
        Bus = NServiceBus.Configure.With()
            .Log4Net()
            .DefaultBuilder()
            .XmlSerializer()
            .MsmqTransport()
                .IsTransactional(false)
                .PurgeOnStartup(false)
            .UnicastBus()
                .ImpersonateSender(false)
            .CreateBus()
            .Start();
    }
}

I have a message handler in the selfhost console that never receives the message from the client:

public class RouteMessageReceivedHandler : IHandleMessages<RouteMessageReceived>
{
    public void Handle(RouteMessageReceived message)
    {
        Trace.TraceInformation(message.GetType().Name);

        Console.Write("you received a message!!" + message.ToString());
    }
}

Can someone share example code how I might handle the messages from my client to the Startup method in my selfhost console app? Thank you very much in advance!

UPDATE: When I send a message from my send/receive NServiceBus.Host (client) to my selfhost bus (server), I receive the following messages:

Debug NServiceBus.Unicast.UnicastBus [(null)] <> - Calling 'HandleBeginMessage' on NServiceBus.SagaPersisters.NHibernate.NHibernateMessageModule

Debug NServiceBus.Unicast.UnicastBus [(null)] <> - Received message SpecimenTrackBus.TrackRouteManager.InternalMessages.RouteMessageReceived, SpecimenTrackBus.TrackRouteManager.InternalMessages, version=1.0.0.0, Culture=neutral, PublicKeyToken=null with ID .###.. from sender OrderManagement@DavisBLptp

Debug NServiceBus.Unicast.UnicastBus [(null)] <> Finished handling message.

Debug NServiceBus.Unicast.UnicastBus [(null)] <> - Calling 'HandleEndMessage' on NServiceBus.SagaPersisters.NHibernate.NHibernateMessageModule

Looks like a message could actually be handled, however, my RouteMessageReceivedHandler is never called. I start each application via its own exe file. Could it be a threading issue?

2

2 Answers

2
votes

You're missing a call to .LoadMessageHandlers() in your configuration

0
votes

Where do you keep your msmq transport configuration? When using the initialization code like this, you will need to provide configuration for the MsmqTransport. In the application config file you need to add lines:

<section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core"/>

<MsmqTransportConfig 
    InputQueue="MyQueue" 
    NumberOfWorkerThreads="1" 
    MaxRetries="5"
    ErrorQueue="error" 
  />