Csharp/C#教程:使用TypesToScan()后,为什么NServiceBus配置中断分享


使用TypesToScan()后,为什么NServiceBus配置中断

我有一个控制台应用程序,您可以在其中指定参数,根据指定的参数将加载各种处理程序。 例如:

prgm.exe nyse prgm.exe nasdaq 

目标是在我的代码中我有INyseHandlersINasdaqHandlers并且在第一种情况下只加载任何扩展前者的处理程序,类似于后者的情况。 目标是让一个程序可以根据其运行方式收听各种或所有来源。 为实现这一目标,我已经设置了如上所述的接口。 然后在我的配置设置中:

 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 handlersToLoad = new List(); 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(c => c.ExistingKernel(Kernel)); configuration.EndpointName(ConfigurationManager.AppSettings[Defaults.Project.DefaultEndPointName]); NServiceBus.Logging.LogManager.Use(); Bus.Create(configuration).Start(); 

在哪里:

 private void AddToHandlerList(List handlersToLoad, Type interfaceType) { List 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(); config.UseTransport(); config.UsePersistence(); config.EnableInstallers(); return config; 

你的例外发生在这里

 localAddress = Address.Parse(Settings.Get("NServiceBus.LocalAddress")); 

设置获取传输设置的“NServiceBus.LocalAddress”kvp。 由于您没有使用“核心”传输(MSMQ),我可能会怀疑您的传输程序集类型需要包含在TypesToScan中,因为:

 ForAllTypes(TypesToScan, t => featureActivator.Add(t.Construct())); 

我在使用SQL Server传输时遇到了类似的问题,当我将程序集列表发送到With(程序集)并且不包含NServiceBus程序集时,传输无法初始化。 一旦我添加了NServiceBus。*程序集,一切都开始工作了。

我想为那些可能找到类似情况的人添加一个答案,我在使用NServiceBus.Testing来测试一个传奇时遇到了这个确切的错误。

当您运行Test.Initialize来设置unit testing时,您实际上可以传递一些BusConfiguration设置,其中您可以指定要扫描的程序集。 我通常使用以下作为标准设置,如果您正在调试测试(至少使用NUnit),它可以防止某些程序集加载问题:

 Test.Initialize(conf => conf.AssembliesToScan(assembly.GetExecutingAssembly())) 

但是,由于我不知道的原因,今天我正在设置一个传奇测试并收到此问题中提到的NServiceBus.LocalAddress错误。 我设法通过向AssembliesToScan添加一个额外的参数来解决这个问题,如下所示:

 Test.Initialize(conf => conf.AssembliesToScan( Assembly.GetExecutingAssembly(), Assembly.GetCallingAssembly())); 

希望这有助于某人。

上述就是C#学习教程:使用TypesToScan()后,为什么NServiceBus配置中断分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/952179.html

(0)
上一篇 2021年11月19日
下一篇 2021年11月19日

精彩推荐