NServicebus订户在收到事件时抛出错误

时间:2015-12-17 11:27:15

标签: asp.net nservicebus msmq

我正在使用NServicebus创建一个示例应用程序。我创建了3个项目 -

  1. 共享项目 - >它包含命令和事件
  2. 发布商 - >它发布了一个名为" placeOrder"
  3. 订阅者 - >它订阅了活动" placeOrder"
  4. 这是我在共享项目中定义的命令 -

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

    以下是我在共享项目中定义的事件 -

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

    这是我的发布商代码 -

     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
        }
    

    以下是发布商的App.Config -

    <?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>
    

    以下是我的订阅代码 -

    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);
                }
            }
        }
    

    这是OrderMessage事件的处理程序 -

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

    这是我对订阅者的配置 -

    <?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>
    

    当我运行代码时 -

    发布者成功发送邮件。但是,没有消息在messagequeue中排队。 此外,订户接收消息然后抛出异常,如下所述

    有谁可以告诉我这里发生了什么问题?

    提前致谢。

    编辑 -

    完整错误堆栈

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

1 个答案:

答案 0 :(得分:4)

通过显式调用LoadMessageHandlers,您可以使其工作,但原因是程序集扫描不正确。

白名单组合

由于组件的白名单,您可能扫描过于严格。您必须包含包含处理程序的程序集,以便注册这些程序集。确保白名单中包含所有 NServiceBus程序集。

黑名单程序集

更好的模式是依赖于默认的程序集扫描,而只是忽略导致问题的程序集。

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

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

是否有明确订阅OrderMessage事件的特定原因,因为默认为自动订阅,这是基于在程序集扫描期间找到的事件处理程序执行的。

事件与命令

旁注:我看到你有:

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

这似乎是一个命令,请阅读我们对不同消息类型的指导: http://docs.particular.net/nservicebus/messaging/messages-events-commands