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

NServicebus subscriber throws an error when it receives an event

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

  1. 共享项目 --> 它包含命令和事件
  2. Publisher--> 它发布一个名为 "placeOrder"
  3. 的事件
  4. 订阅者 --> 它订阅事件 "placeOrder"

这是我在共享项目中定义的命令 -

    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>

当我运行代码-

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

谁能告诉我这是怎么回事?

提前致谢。

编辑-

错误堆栈已满

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\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\LoadHandlersBehavior.cs:line 29
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ApplyIncomingMessageMutatorsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\MessageMutator\ApplyIncomingMessageMutatorsBehavior.cs:line 23
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ExecuteLogicalMessagesBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Messages\ExecuteLogicalMessagesBehavior.cs:line 24
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.CallbackInvocationBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\CallbackInvocationBehavior.cs:line 22
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.DeserializeLogicalMessagesBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Messages\DeserializeLogicalMessagesBehavior.cs:line 47
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ApplyIncomingTransportMessageMutatorsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\MessageMutator\ApplyIncomingTransportMessageMutatorsBehavior.cs:line 20
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.SubscriptionReceiverBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Subscriptions\MessageDrivenSubscriptions\SubscriptionReceiverBehavior.cs:line 31
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.UnitOfWorkBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\UnitOfWork\UnitOfWorkBehavior.cs:line 42
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ChildContainerBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Behaviors\ChildContainerBehavior.cs:line 17
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.<>c__DisplayClass4_0.<InvokeNext>b__0() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 94
   at NServiceBus.ProcessingStatisticsBehavior.Invoke(IncomingContext context, Action next) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Monitoring\ProcessingStatisticsBehavior.cs:line 23
   at NServiceBus.BehaviorChain`1.InvokeNext(T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 107
   at NServiceBus.BehaviorChain`1.Invoke() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\BehaviorChain.cs:line 52
   at NServiceBus.Pipeline.PipelineExecutor.Execute[T](BehaviorChain`1 pipelineAction, T context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 129
   at NServiceBus.Pipeline.PipelineExecutor.InvokePipeline[TContext](IEnumerable`1 behaviors, TContext context) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 85
   at NServiceBus.Pipeline.PipelineExecutor.InvokeReceivePhysicalMessagePipeline() in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Pipeline\PipelineExecutor.cs:line 102
   at NServiceBus.Unicast.UnicastBus.TransportMessageReceived(Object sender, TransportMessageReceivedEventArgs e) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\UnicastBus.cs:line 826
   at NServiceBus.Unicast.Transport.TransportReceiver.OnTransportMessageReceived(TransportMessage msg) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 411
   at NServiceBus.Unicast.Transport.TransportReceiver.ProcessMessage(TransportMessage message) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 344
   at NServiceBus.Unicast.Transport.TransportReceiver.TryProcess(TransportMessage message) in C:\BuildAgent\work06e2123f54fce4\src\NServiceBus.Core\Unicast\Transport\TransportReceiver.cs:line 228
   at NServiceBus.Transports.Msmq.MsmqDequeueStrategy.Action() in C:\BuildAgent\work06e2123f54fce4\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).

通过显式调用 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