5
votes

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?

1
TrackEventPublisher Config: <configuration> <configSections> <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" /> <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" /> </configSections> <MsmqTransportConfig InputQueue="TrackEventPublisher" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5" /> <UnicastBusConfig ForwardReceivedMessagesTo=""> <MessageEndpointMappings> </MessageEndpointMappings> </UnicastBusConfig> </configuration>EnLaCucha
Is your TrackEventMessage in its own project called TrackEventPublisher.TrackEventPublisher.InternalMessages?Udi Dahan
Yes, TrackEventMessage is in its own project as you mention. Udi, I actually followed your pub/sub training video at vimeo.com/29659143. Instead of trying to use a send/receive NServiceBus host as my message sender, I created a WPF application similar to the web app in your demo. It works great!! I am attempting to use a WPF application subscriber. Can you take a look at a question I posted in another thread at: stackoverflow.com/questions/9550902/… Thanks for your help! Hurrah for NServiceBus!EnLaCucha
You seem to be mixing up two hosting models. You have the endpoint config class which is ended to be used with NServiceBus.Host.exe but then you have your own console application as a host. Try using the built-in host to check the rest of your configuration before moving to your own hosting.Udi Dahan
Would you mind posting this as the answer so that we can close this one?Andreas Öhlund

1 Answers

2
votes

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. :)