【问题标题】:NServiceBus - Scanning all assembliesNServiceBus - 扫描所有程序集
【发布时间】:2016-03-08 15:53:15
【问题描述】:

NServiceBus.dll - 版本 5.2.9 和 NServiceBus.Host - 版本 6.0.0

我正在开发一个带有可插入插件的工作流应用程序。

在我的解决方案中,我有一个使用 NServiceBus.Host.exe 托管的 NServiceBus 主机程序集。为了防止扫描,我在 NServiceBus.Host.exe.config 中定义了 EndpointConfigurationType。

<appSettings>
 <add key="EndpointConfigurationType" value="Libra.Workflow.Host.EndpointConfig, Libra.Workflow.Host" />
</appSettings>

我已经验证了这个配置正在被使用,因为如果我输入了一些未知的类型,我会得到一个错误,还因为我的 EndpointConfig 类 在任何扫描发生之前被实例化

在这个类的Customize方法中我已经添加了

public void Customize(BusConfiguration cfg)
{
  cfg.AssembliesToScan(AllAssemblies.Matching("Libra.Workflow.Messages.dll"));
  ...
}

现在,当我运行这个项目时,我收到一个错误,因为 NServiceBus 正在扫描所有程序集,并且由于 System.AddIn 的性质,无法扫描某些程序集!

此扫描发生在 Libra.Workflow.Host 被实例化之后但在调用 Customize 方法之前。这是此扫描的调用堆栈:

at NServiceBus.Hosting.Helpers.AssemblyScanner.ScanAssembly(String assemblyPath, AssemblyScannerResults results) in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Hosting\Helpers\AssemblyScanner.cs:line 153
at NServiceBus.Hosting.Helpers.AssemblyScanner.GetScannableAssemblies() in C:\BuildAgent\work\3206e2123f54fce4\src\NServiceBus.Core\Hosting\Helpers\AssemblyScanner.cs:line 63
at NServiceBus.GenericHost..ctor(IConfigureThisEndpoint specifier, String[] args, List`1 defaultProfiles, String endpointName, IEnumerable`1 scannableAssembliesFullName) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\GenericHost.cs:line 33
at NServiceBus.Hosting.Windows.WindowsHost..ctor(Type endpointType, String[] args, String endpointName, IEnumerable`1 scannableAssembliesFullName) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\WindowsHost.cs:line 21
at NServiceBus.Hosting.Windows.HostServiceLocator.DoGetInstance(Type serviceType, String key) in c:\BuildAgent\work\a3de8759ee491634\src\NServiceBus.Hosting.Windows\HostServiceLocator.cs:line 31
at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49

我得到的错误信息是:

Could not enumerate all types for
'C:\msc\Trunk\Libra.Workflow\Build\Libra.Workflow.Host\AddIns\Libra.Workflow\Libra.Workflow.Processors.dll'

为什么 NServiceBus 会扫描这个 DLL,我该如何阻止它?

注意:由于这是一个插件 DLL,在 Libra.Workflow.Host 或任何其他相关程序集中都没有对它的引用,因此 NServiceBus 绝对没有理由必须触摸它。

【问题讨论】:

  • 是否需要将 Libra.Workflow.Processors.dll 依赖添加到程序集扫描中?
  • 加载项的问题是它们的一些依赖项位于其他地方,更具体地说是在运行主机的子文件夹中。当我构建一个加载项时,我引用了一个 AddInView,但我设置了 Copy Local = False,因此 AddInView.dll 不会最终出现在 AddIn 文件夹中。结构是这样的:.\AddIns\Libra.Workflow\Libra.Workflow.Processors.dll.\AddInViews\Libra.Workflow.Processors.Pipeline.AddInView.dll这第二个dll是扫描器找不到的依赖。不包含在插件中的原因是所有插件共享同一个 AddInView.dll
  • 我找到了一个临时解决方法,但非常想阻止 NServiceBus 扫描。解决方法是将额外的子文件夹添加到 .config 文件中的程序集探测中 &lt;runtime&gt; &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt; &lt;probing privatePath="AddInSideAdapters;AddInViews;Contracts;HostSideAdapters"/&gt; &lt;/assemblyBinding&gt; &lt;/runtime&gt;
  • 我很困惑,你告诉 NServiceBus 扫描 AssembliesToScan(AllAssemblies.Matching("Libra.Workflow.Messages.dll")); 但你说你不想让它扫描??

标签: nservicebus nservicebus5


【解决方案1】:

限制由 NServiceBus.Host 完成的程序集扫描的一种方法是使用 /scannedAssemblies 开关。一个警告是明确传递 NServiceBus.Core 和 NServiceBus.Host 程序集:

NServiceBus.Host.exe /scannedAssemblies:"NServiceBus.Core, Version=5.0.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c" /scannedAssemblies:"NServiceBus.Host, Version=6.0.0.0, Culture=neutral, PublicKeyToken=9fc386479f8a226c"

此命令将扫描那些 NServiceBus 程序集和通过 EndpointConfigurationType 应用程序设置指定的程序集。如果你想指定额外的程序集(比如你的 Libra.Workflow.Messages),你可以添加额外的 /scannedAssemblies 开关。

有关详细信息,请参阅此文档页面:http://docs.particular.net/nservicebus/hosting/nservicebus-host/#configuring-the-endpoint-controlling-assembly-scanning-using-the-command-line

【讨论】:

  • 谢谢,正是我需要的,这适用于 .exe 模式和 NTService 模式。你知道是否有我必须添加的核心程序集,比如 NServiceBus.Core?
  • 您还应该指定您正在使用的任何扩展程序集。例如,如果您使用 MSMQ 以外的传输,您也应该添加传输组装。有关程序集扫描的更多详细信息,请参阅此文档:docs.particular.net/nservicebus/hosting/…
  • 再次感谢您。正是我需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多