简体   繁体   English

如何使用NServiceBus部分加载处理程序?

[英]How can I partially load handlers with NServiceBus?

I am running a console application within which there is a multitude of handlers extending <IHandleMessages> . 我正在运行一个控制台应用程序,其中有许多扩展<IHandleMessages>的处理程序。 When I run the application, I want to specify command line arguments such that only some of those handlers will be running. 在运行应用程序时,我想指定命令行参数,以便仅其中一些处理程序将运行。 I am working in part with existing code, so I am slightly confused, but it seems that the bus is configured as such: 我正在使用现有代码,因此我有些困惑,但是似乎总线是这样配置的:

public static BusConfiguration MyMessageConventions(this BusConfiguration config)
{
    config.UseSerialization<JsonSerializer>();
    config.UseTransport<RabbitMQTransport>();
    config.UsePersistence<InMemoryPersistence>();

    var conventionsBuilder = config.Conventions();
    conventionsBuilder.DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("DE.STEP.Messages") && t.Namespace.EndsWith("Events"));
    return config;
}

That's all there is to it, and as I understand that spares me having to implement an IMessage interface. 这就是全部内容,据我所知,这使我不必实施IMessage接口。 Now as I see it, NServiceBus will scan for all handlers in my console project, so I am wondering how I can configure it to not do that (while leaving all other behavior in place), and only load up the handlers I specify? 现在,如我所见,NServiceBus将扫描我的控制台项目中的所有处理程序,因此我想知道如何配置它使其不执行此操作(同时保留所有其他行为),并仅加载指定的处理程序?

The easiest solution would be to split your messages and handlers to multiple projects. 最简单的解决方案是将您的消息和处理程序拆分到多个项目。 Usually in scenarios like yours, there is some sort of logical separation between these messages/handlers groups that you want to control by your configuration (or command line) parameters. 通常,在像您这样的方案中,您希望通过配置(或命令行)参数控制这些消息/处理程序组之间存在某种逻辑分离。

NServiceBus scans all assemblies it finds in the application folder to find all handlers and other marker interfaces. NServiceBus扫描在应用程序文件夹中找到的所有程序集,以找到所有处理程序和其他标记接口。 You can limit the list of assemblies by using (V5): 您可以使用(V5)来限制部件列表:

configuration.AssembliesToScan(myListOfAssemblies);

You can construct the list of assemblies based on your configuration parameters. 您可以根据配置参数构造程序集列表。 If you have different deployment where you want to use different set of handlers, you can just deploy those assemblies that you need at that particular installation. 如果在不同的部署中要使用不同的处理程序集,则可以仅部署在特定安装中所需的那些程序集。

You can use one assembly or set of assemblies for your messages and these are configured using your DefiningEventsAs call, and have separate assemblies for handlers. 您可以为消息使用一个程序集或一组程序集,这些都使用DefiningEventsAs调用进行配置,并且具有用于处理程序的单独程序集。

The documentation about NServiceBus assembly scanning can be found here . 有关NServiceBus程序集扫描的文档可在此处找到。

It sounds like you want to turn off auto subscription: 听起来您想关闭自动订阅:

config.DisableFeature<AutoSubscribe>();

You can then individually subscribe to messages: 然后,您可以单独订阅消息:

Bus.Subscribe<MyMessage>();

You probably want to first unsubscribe to ALL messages at startup (Bus.Unsubscribe), then re-subscribe to the ones you are interested in. Otherwise your subscriptions will still be present from the last time you ran the application. 您可能希望首先在启动时取消订阅所有消息(Bus.Unsubscribe),然后重新订阅您感兴趣的消息。否则,从上次运行该应用程序开始,您的订阅仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用实现泛型的消息测试NServicebus消息处理程序 - Can't test NServicebus Message Handlers with Messages which implement generics 如何在V5中配置NServicebus V4 ConfigurarionComplete事件处理程序? - How configure NServicebus V4 ConfigurarionComplete event handlers in V5? 如何避免加入NServiceBus的环境TransactionScope? - How can I avoid joining NServiceBus' ambient TransactionScope? 如何使用 Jaeger 获取 Nservicebus 的踪迹? - How can I get traces of Nservicebus using Jaeger? 如何处理一个以上的NServiceBus项目? - How can I handle more than one NServiceBus project in process? 如何在nservicebus中预处理消息? - How can I pre-process a message in nservicebus? 我如何使用NServiceBus来做竞争消费者 - How can i use NServiceBus to do Competing Consumers NServiceBus指定处理程序执行的顺序 - NServiceBus specify order of Handlers execution NServiceBus新手-如何强制通用主机先加载订阅者,然后处理消息? - NServiceBus newbie - how do I force generic host to load first subscribers, then handle messages? 我有一个部分查询和部分方法的 LINQ 语句。 我如何使用其中一个来做到这一点? - I have a LINQ statement that is partially query and partially method. How can I do this using one or the other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM