1

我目前正在开发一个示例项目,我在后端使用 NServiceBus 来发布事件。然后后端应该通过一组处理程序处理自己的事件。当我为每个事件实现一个特定的处理程序时,这很好用,如下所示:

public sealed class MyEventHandler : IHandleMessages<MyEvent> { }

public sealed class OtherEventHandler : IHandleMessages<OtherEvent> { }

然而,由于这些处理程序只是将事件分派给另一个处理器,我想清理一下并只实现一个通用消息处理程序:

public sealed class GenericHandler : IHandleMessages<object> { }

不幸的是,只有在项目中存在另一个特定处理程序的消息类型才会调用通用处理程序。换句话说,当我将 MyEventHandler 留在源中时,GenericHandler 正确接收 MyEvent 事件,但当我删除此(现在已过时)处理程序时停止接收此消息。我的目标实际上是从项目中删除所有特定的处理程序,并且只使用单个 GenericHandler。我错过了配置的基本步骤吗?

NServiceBus 配置如下所示:

<UnicastBusConfig>
  <MessageEndpointMappings>
    <add Assembly="Kingo.Samples.Chess.Api" Endpoint="kingo.samples.chess" />      
  </MessageEndpointMappings>
</UnicastBusConfig>

此外:

  • 上面提到的 Api-assembly 包含了所有需要发布和处理的事件。
  • 我用NServiceBus的ICommand和标记接口标记了所有消息。IEvent
  • 我将 NServiceBus V5 (Core) 与 NServiceBus.Host V6 结合使用。
  • 我的 EndpointConfig 中有以下自定义总线配置:

    void IConfigureThisEndpoint.Customize(BusConfiguration configuration)
    {                                     
    
        configuration.AssembliesToScan(GetAssembliesToScan("*Chess.Api.dll", "*Chess.dll"));            
        configuration.UsePersistence<InMemoryPersistence>();
        configuration.UseContainer<UnityBuilder>();
        configuration.UseSerialization<JsonSerializer>();
    
        configuration.Conventions().DefiningEventsAs(type => type.Name.EndsWith("Event"));
    }
    
4

1 回答 1

0

您的配置将事件定义为以“事件”结尾的任何类。“对象”不符合此标准,因此您不会在通用处理程序中收到消息。

您需要改用 IEvent :

public sealed class GenericHandler : IHandleMessages<IEvent> { }
于 2016-01-01T18:16:07.930 回答