Search code examples
asp.netnservicebusmsmq

NServicebus subscriber throws an error when it receives an event


i am crating a sample application with NServicebus. I have created 3 projects-

  1. Shared project --> It contains commands and events
  2. Publisher--> It publishes an event called "placeOrder"
  3. Subscriber --> It subscribe for the event "placeOrder"

Here is my Command defined in shared project -

    public class OrderCommand : ICommand
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }

Here is the event I have defined in shared project -

    public class OrderMessage : IEvent
    {
        public Guid Id { get; set; }
        public string Description { get; set; }
    }

Here is my publisher code -

 public class Program
    {
        public static void Main(string[] args)
        {
            BusConfiguration busConfiguration = new BusConfiguration();
            busConfiguration.EndpointName("publisher");
            busConfiguration.AssembliesToScan(typeof(Shared.Messages.OrderMessage).Assembly, typeof(NServiceBus.MsmqTransport).Assembly);

            //busConfiguration.UseTransport<RabbitMQTransport>();
            busConfiguration.UseTransport<MsmqTransport>();
            busConfiguration.UseSerialization<JsonSerializer>();
            busConfiguration.UsePersistence<InMemoryPersistence>();
            busConfiguration.EnableInstallers();

            using (IBus bus = Bus.Create(busConfiguration).Start())
            {
                SendOrder(bus);
            }
        }

        #region SendOrder

        static void SendOrder(IBus bus)
        {
            Console.WriteLine("Press enter to send a message");
            Console.WriteLine("Press any key to exit");

            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                Console.WriteLine();

                if (key.Key != ConsoleKey.Enter)
                {
                    break;
                }

                OrderMessage placeOrder = new OrderMessage { Description = "TEST", Id = Guid.NewGuid() };
                bus.Publish(placeOrder);

                Console.WriteLine("Sent a new message with id: {0}", placeOrder.Id.ToString());
            }
        }

        #endregion
    }

Here is the App.Config of publisher-

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />    
  </configSections>
  <MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
  <connectionStrings>
    <add name="NServiceBus/Transport"
         connectionString="host=localhost"/>
  </connectionStrings>
</configuration>

Here is my code for subscriber -

public class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                BusConfiguration busConfiguration = new BusConfiguration();                
                busConfiguration.AssembliesToScan(typeof(Shared.Messages.OrderMessage).Assembly, typeof(NServiceBus.MsmqTransport).Assembly);

                //busConfiguration.UseTransport<RabbitMQTransport>();
                busConfiguration.UseTransport<MsmqTransport>();
                busConfiguration.UseSerialization<JsonSerializer>();
                busConfiguration.UsePersistence<InMemoryPersistence>();
                busConfiguration.EnableInstallers();

                ////busConfiguration.LoadMessageHandlers<First<OrderMessage>>();

                using (IBus bus = Bus.Create(busConfiguration).Start())
                {
                    bus.Subscribe<OrderMessage>();
                    Console.WriteLine("Press any key to exit");
                    Console.ReadKey();
                }
            }
            catch(Exception e)
            {
                Console.Write(e.Message);
            }
        }
    }

Here is the handler for OrderMessage event-

  public class OrderCreatedHandler : IHandleMessages<OrderMessage>
    {
        void IHandleMessages<OrderMessage>.Handle(OrderMessage message)
        {
            Console.WriteLine(@"Handling: OrderPlaced for Order Id: {0}", message.Id);
        }
    }

This is my configuration for subscriber-

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" />
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core"/>
  </configSections>
  <MessageForwardingInCaseOfFaultConfig ErrorQueue="error" />
  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Assembly="Shared" Endpoint="publisher" />
    </MessageEndpointMappings>
  </UnicastBusConfig>
  <connectionStrings>
    <add name="NServiceBus/Transport"
         connectionString="host=localhost"/>
  </connectionStrings>
</configuration>

When I am running the code -

the publisher send the message successfully. However no message is queued in messagequeue. Further, the subscriber, receives the message and then throw an exception as mentioned below

Can anybody tell me what is wrong going on here?

Thanks in advance.

EDIT-

Full error stack

2015-12-18 15:27:30.614 INFO  NServiceBus.Unicast.Transport.TransportReceiver Failed to process message
System.InvalidOperationException: No handlers could be found for message type: Shared.Events.OrderMessage
   at NServiceBus.LoadHandlersBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\LoadHandlersBehavior.cs:line 29
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ApplyIncomingMessageMutatorsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\MessageMutator\ApplyIncomingMessageMutatorsBehavior.cs:line 23
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ExecuteLogicalMessagesBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Messages\ExecuteLogicalMessagesBehavior.cs:line 24
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.CallbackInvocationBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\CallbackInvocationBehavior.cs:line 22
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.DeserializeLogicalMessagesBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Messages\DeserializeLogicalMessagesBehavior.cs:line 47
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ApplyIncomingTransportMessageMutatorsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\MessageMutator\ApplyIncomingTransportMessageMutatorsBehavior.cs:line 20
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.SubscriptionReceiverBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Subscriptions\MessageDrivenSubscriptions\SubscriptionReceiverBehavior.cs:line 31
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.UnitOfWorkBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\UnitOfWork\UnitOfWorkBehavior.cs:line 42
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ChildContainerBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\ChildContainerBehavior.cs:line 17
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ProcessingStatisticsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Monitoring\ProcessingStatisticsBehavior.cs:line 23
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.Invoke() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 52
   at NServiceBus.Pipeline.PipelineExecutor.Execute[T](BehaviorChain`1 pipelineAction, T context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 129
   at NServiceBus.Pipeline.PipelineExecutor.InvokePipeline[TContext](IEnumerable`1 behaviors, TContext context) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 85
   at NServiceBus.Pipeline.PipelineExecutor.InvokeReceivePhysicalMessagePipeline() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 102
   at NServiceBus.Unicast.UnicastBus.TransportMessageReceived(Object sender, TransportMessageReceivedEventArgs e) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 826
   at NServiceBus.Unicast.Transport.TransportReceiver.OnTransportMessageReceived(TransportMessage msg) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 411
   at NServiceBus.Unicast.Transport.TransportReceiver.ProcessMessage(TransportMessage message) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 344
   at NServiceBus.Unicast.Transport.TransportReceiver.TryProcess(TransportMessage message) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 228
   at NServiceBus.Transports.Msmq.MsmqDequeueStrategy.Action() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Transports\Msmq\MsmqDequeueStrategy.cs:line 266
The thread 0x3584 has exited with code 0 (0x0).
2015-12-18 15:27:30.645 ERROR NServiceBus.Faults.Forwarder.FaultManager SLR has failed to resolve the issue with message e13fa75c-2bc6-4ac6-978f-a57200fe77e1 and will be forwarded to the error queue at error@PC
The thread 0x17a8 has exited with code 0 (0x0).
The program '[13440] dnx.exe: Program Trace' has exited with code 0 (0x0).
The program '[7692] dnx.exe: Program Trace' has exited with code 0 (0x0).
The program '[13440] dnx.exe' has exited with code -1 (0xffffffff).
The program '[7692] dnx.exe' has exited with code -1 (0xffffffff).

Solution

  • By explicitly calling LoadMessageHandlers you made it work but the cause is incorrect assembly scanning.

    Whitelist asssemblies

    You are probably scanning too strict due to the whitelisting of assemblies. You must include the assembly that is containing your handlers so that these are registered. Do make sure that all NServiceBus assemblies are included when whitelisting.

    Blacklist assemblies

    A better pattern is to rely on the default assembly scanning and only to ignore assemblies that cause issues.

    http://docs.particular.net/nservicebus/hosting/assembly-scanning

    IExcludesBuilder excludesBuilder = AllAssemblies
        .Except("MyAssembly1.dll")
        .And("MyAssembly2.dll");
    busConfiguration.AssembliesToScan(excludesBuilder);
    

    Is there a specific reason that you explicitly subscribe to the OrderMessage event as the default is auto subscription which is performed based on the event handlers that have been found during the assembly scanning.

    Events vs Commands

    Side note: I see that you have :

    OrderMessage placeOrder = new OrderMessage { Description = "TEST", Id = Guid.NewGuid() };
    bus.Publish(placeOrder);
    

    This seems like a command, please read our guidance on different messages types: http://docs.particular.net/nservicebus/messaging/messages-events-commands