Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No destination specified for message, but my NServiceBus configuration appears to setup correctly

Tags:

c#

nservicebus

I have created a standard NServiceBus publish subscribe program. I keep getting the following error when sending a message to my publisher NServicBus host:

No destination specified for message TrackEventPublisher.TrackEventPublisher.InternalMessages.TrackEventMessages. Message cannot be sent. Check the UnicastBusConfig section in your config file and ensure that a MessageEndpointMapping exists for the message type.

Well.... the MessageEndpointMapping exists!

Here is my test console application code I use to test the MessagePublisher class:

class Program
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Click enter to submit message.");
         Console.ReadLine();

         string aMessage = "Please Work!!!!!!!!";
         MessagePublisher publisher = new MessagePublisher();
         publisher.PublishEventMessage(aMessage);
      }
   }

Here is my message sender class which sends a message to another NServiceBus publish server to be published:

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

      public MessagePublisher()
      {
         BusInitializer.Init();
         Bus = BusInitializer.Bus;
      }

      public void PublishEventMessage(string message)
      {
         Bus.Send(new TrackEventMessages(message));
      } 
   }

My bus initializer:

   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()
                 .LoadMessageHandlers()
                 .ImpersonateSender(false)
             .CreateBus()
             .Start();
      }
   }

My message class:

namespace TrackEventPublisher.TrackEventPublisher.InternalMessages
{
    public class TrackEventMessages : IMessage
    {
       public string HelloWorldMessage { get; set; }

       public TrackEventMessages(string message)
       {
          HelloWorldMessage = message;
       }
    }
}

And finally, my configuration for the message publisher:

namespace TrackEventPublisher.PublishManager
{
    public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
    {
    }
}

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

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

  <UnicastBusConfig ForwardReceivedMessagesTo="">
    <MessageEndpointMappings>
      <add Messages="TrackEventPublisher.TrackEventPublisher.InternalMessages" Endpoint="TrackEventPublisher" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
</configuration>

The configuration was created by the NServiceBus auto-generator. The configuration appears to be correct. Does anyone have any idea why I get the InvalidOperationException "No destination specified for message" when sending the message via:

    Bus.Send(new TrackEventMessages(message));

Thanks in advance. I have spend waaaay too much time on this one.

* Update **

I may be setting up the Bus incorrectly from my MessagePubliser class. My goal is to instantiate the MessagePublisher class from another application (wpf, console, etc). That is why I am using a BusInitializer class. However, the Bus I am creating does not correlate with my app.config in the MessagePublisher class.
Does anyone have a good idea how to instantiate the MessagePublisher class so my bus recognizes the app.config? Perhaps using IWantToRunAtStartup?

like image 310
EnLaCucha Avatar asked Nov 05 '22 04:11

EnLaCucha


1 Answers

The idea was to use a WPF application to initialize the bus and send information to a publisher host. I watched Udi's pub/sub demo video (see NServiceBus website for online training) which described using a send only NServiceBus host as a web application to send messages to a publisher. I used a WPF application instead of the web app, and it worked like a charm.

Here is some code from my application that may help someone else.

App.Config:

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

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

  <UnicastBusConfig ForwardReceivedMessagesTo="">
    <MessageEndpointMappings>
      <add Messages="TrackEventPublisher.EventPublisher.InternalMessages" Endpoint="EventPublisher" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
</configuration>

App.xaml.cs:

public partial class App : Application
{
  public static IBus Bus { get; private set; }

  protected override void OnStartup(StartupEventArgs e)
  {
     base.OnStartup(e);

     Bus = NServiceBus.Configure.With()
         .DefineEndpointName("DemoExecutive")
         .Log4Net()
         .DefaultBuilder()
         .XmlSerializer()
         .MsmqTransport()
             .IsTransactional(false)
             .PurgeOnStartup(false)
         .UnicastBus()
             .ImpersonateSender(false)
         .CreateBus()
         .Start(() => Configure.Instance.ForInstallationOn<NServiceBus.Installation.Environments.Windows>().Install());
  }
}

MainWindow.xaml.cs: (note: you use App.Bus.Send() to send the message.)

  private void button1_Click(object sender, RoutedEventArgs e)
  {
     const long aNumber = 999999;
     var eventWhen = DateTime.Now;
     const string eventWhere = "Generic Kitchen";
     const string eventType = "Breakfast";
     const string aStatus = "Broken Toaster";

     var genericEventMessage = new GenericEventMessage
                                                  {
                                                     Number = aNumber,
                                                     EventWhen = eventWhen,
                                                     EventWhere = eventWhere,
                                                     EventType = eventType,
                                                     Status = aStatus
                                                  };

     App.Bus.Send(new TrackEventMesage(genericEventMessage));
  }

Sorry I am so late posting the answer Andreas. I goes to show how often I look back at my posts. :)

like image 196
EnLaCucha Avatar answered Nov 14 '22 23:11

EnLaCucha