【发布时间】:2015-03-16 15:47:19
【问题描述】:
我有一个控制台应用程序,您可以在其中指定参数,根据指定的参数,将加载各种处理程序。例如:
prgm.exe nyse
prgm.exe nasdaq
目标是在我的代码中我有INyseHandlers 和INasdaqHandlers,在第一种情况下,只有扩展前者的任何处理程序被加载,对于后者的情况类似。目标是拥有一个程序,它可以根据其运行方式收听各种或所有来源。为了实现这一点,我已经设置了我上面提到的接口。然后在我的配置设置中:
var configuration = new BusConfiguration();
configuration.InitializeStepBusConventions(); // extension method, not the problem
// Load all the handlers specified in command line arguments
if (!Args.Contains("any") && Args.Length != 0)
{
List<Type> handlersToLoad = new List<Type>();
foreach (var argument in Args)
{
Console.WriteLine("Adding {0} subscribers to loaded handlers. . .", argument.ToUpper());
switch (argument)
{
case "nyse":
AddToHandlerList(handlersToLoad, typeof(INyseProcessor));
break;
case "nasdaq":
AddToHandlerList(handlersToLoad, typeof(INasdaqProcessor));
break;
}
}
configuration.TypesToScan(handlersToLoad);
}
configuration.UseContainer<NinjectBuilder>(c => c.ExistingKernel(Kernel));
configuration.EndpointName(ConfigurationManager.AppSettings[Defaults.Project.DefaultEndPointName]);
NServiceBus.Logging.LogManager.Use<NLogFactory>();
Bus.Create(configuration).Start();
在哪里:
private void AddToHandlerList(List<Type> handlersToLoad, Type interfaceType)
{
List<Type> classesWhichExtendInterface = Assembly.GetExecutingAssembly().GetTypes().Where(t => interfaceType.IsAssignableFrom(t)).ToList();
classesWhichExtendInterface.Remove(interfaceType);
handlersToLoad.AddRange(classesWhichExtendInterface);
}
类型按预期加载,List 很好。但是当我运行它并到达Bus.Start 行时,我收到以下错误:
The given key (NServiceBus.LocalAddress) was not present in the dictionary.
如果不加载类型,默认行为可以正常工作,并且程序集中的所有处理器都已加载。为什么运行TypesToScan() 行后出现此错误?
编辑:这是扩展方法:
config.UseSerialization<JsonSerializer>();
config.UseTransport<RabbitMQTransport>();
config.UsePersistence<InMemoryPersistence>();
config.EnableInstallers();
return config;
【问题讨论】:
-
我认为通过这样做,您只允许它获取您的处理程序,并且删除所有其他内容,例如消息、传输、配置。
-
我认为这是一个公平的假设,似乎
TypesToScan假设您是从头开始并完全放弃了默认设置的其余部分。知道如何检索使其正常运行所需的那些吗? -
我不确定。我建议在列表已填充时删除不需要的处理程序,但我不确定何时放置此代码的正确交叉点。我仍然认为将处理程序拆分到两个不同的项目是最简单的。
-
该错误消息很奇怪,当在内存中的
Overrides或Defaults设置集合中找不到配置设置时会抛出该错误消息。只是出于好奇,您使用的是什么传输机制,因为NServiceBus.Local不是核心组件中的关键(除非它隐藏在某个地方)? -
使用 RabbitMQ 作为传输层
标签: c# nservicebus