为什么我需要任何 Saga 存储?

Why do I need any Saga storage?

我正在努力处理启动期间从 NServiceBus.Host 收到的错误消息,但不确定我哪里出错了。我最初在一个大型项目中遇到了这个问题,但我似乎能够从头开始重现这个问题。

在 VS2012 中,从一个新的 C# Class 库项目开始,为 .NET 4.6.2 设置。然后添加 NuGet 包 - NServiceBus (6.0.0)、NServiceBus.Host (7.0.1) 和 NServiceBus.NHibernate (7.2.0).

然后编辑EndpointConfiguration class如下:

using NServiceBus.Features;
using NServiceBus.Persistence;

namespace NSB6_Pure
{
  using NServiceBus;

  public class EndpointConfig : IConfigureThisEndpoint
  {
    public void Customize(EndpointConfiguration endpointConfiguration)
    {
      //TODO: NServiceBus provides multiple durable storage options, including SQL Server, RavenDB, and Azure Storage Persistence.
      // Refer to the documentation for more details on specific options.
      endpointConfiguration.UsePersistence<NHibernatePersistence, StorageType.Timeouts>();
      endpointConfiguration.DisableFeature<MessageDrivenSubscriptions>();
      endpointConfiguration.DisableFeature<Sagas>();
      // NServiceBus will move messages that fail repeatedly to a separate "error" queue. We recommend
      // that you start with a shared error queue for all your endpoints for easy integration with ServiceControl.
      endpointConfiguration.SendFailedMessagesTo("error");

      // NServiceBus will store a copy of each successfully process message in a separate "audit" queue. We recommend
      // that you start with a shared audit queue for all your endpoints for easy integration with ServiceControl.
      endpointConfiguration.AuditProcessedMessagesTo("audit");
    }
  }
}

然后编译(在本例中为调试版本)并从命令提示符尝试 运行 NServiceBus.Host.exe 目录中的 bin\Debug。在我的例子中,我得到了这个输出,其中大部分是红色的:

2017-04-28 12:46:11.876 INFO  DefaultFactory Logging to 'C:\blah\bin\Debug\' with level Info
2017-04-28 12:46:13.446 FATAL NServiceBus.LicenseManager Your license has expire
d! You can renew it at https://particular.net/licensing.
2017-04-28 12:46:13.561 ERROR NServiceBus.GenericHost Exception when starting en
dpoint.
System.InvalidOperationException: In order to use NServiceBus with NHibernate yo
u need to provide at least one connection string. You can do it via (in order of
 precedence):
 * specifying 'NServiceBus/Persistence/NHibernate/Saga' connection string for th
e Saga persister
 * specifying 'NServiceBus/Persistence' connection string that applies to all pe
rsisters
 * specifying 'NServiceBus/Persistence/connection.connection_string' or 'NServic
eBus/Persistence/connection.connection_string_name' value in AppSettings or your
 NHibernate configuration file.
For most scenarios the 'NServiceBus/Persistence' connection string is the best o
ption.
   at NServiceBus.Persistence.NHibernate.NHibernateConfigurationBuilder.Validate
ConfigurationViaConfigFile(Configuration configuration, String configPrefix) in
C:\BuildAgent\work35de308b2f3016\src\NServiceBus.NHibernate\Internal\NHiberna
teConfigurationBuilder.cs:line 130
   at NServiceBus.Features.NHibernateStorageSession.Setup(FeatureConfigurationCo
ntext context) in C:\BuildAgent\work35de308b2f3016\src\NServiceBus.NHibernate
\SynchronizedStorage\NHibernateStorageSession.cs:line 45
   at NServiceBus.Features.FeatureActivator.ActivateFeature(FeatureInfo featureI
nfo, List`1 featuresToActivate, IConfigureComponents container, PipelineSettings
 pipelineSettings) in C:\Build\src\NServiceBus.Core\Features\FeatureActivator.cs
:line 194
   at NServiceBus.Features.FeatureActivator.SetupFeatures(IConfigureComponents c
ontainer, PipelineSettings pipelineSettings) in C:\Build\src\NServiceBus.Core\Fe
atures\FeatureActivator.cs:line 57
   at NServiceBus.InitializableEndpoint.<Initialize>d__1.MoveNext() in C:\Build\
src\NServiceBus.Core\InitializableEndpoint.cs:line 50
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at NServiceBus.GenericHost.<Start>d__1.MoveNext() in C:\BuildAgent\work\fc89e
968acb99302\src\NServiceBus.Hosting.Windows\GenericHost.cs:line 48
2017-04-28 12:46:13.582 ERROR NServiceBus.Hosting.Windows.WindowsHost Start fail
ure
System.InvalidOperationException: In order to use NServiceBus with NHibernate yo
u need to provide at least one connection string. You can do it via (in order of
 precedence):
 * specifying 'NServiceBus/Persistence/NHibernate/Saga' connection string for th
e Saga persister
 * specifying 'NServiceBus/Persistence' connection string that applies to all pe
rsisters
 * specifying 'NServiceBus/Persistence/connection.connection_string' or 'NServic
eBus/Persistence/connection.connection_string_name' value in AppSettings or your
 NHibernate configuration file.
For most scenarios the 'NServiceBus/Persistence' connection string is the best o
ption.
   at NServiceBus.Persistence.NHibernate.NHibernateConfigurationBuilder.Validate
ConfigurationViaConfigFile(Configuration configuration, String configPrefix) in
C:\BuildAgent\work35de308b2f3016\src\NServiceBus.NHibernate\Internal\NHiberna
teConfigurationBuilder.cs:line 130
   at NServiceBus.Features.NHibernateStorageSession.Setup(FeatureConfigurationCo
ntext context) in C:\BuildAgent\work35de308b2f3016\src\NServiceBus.NHibernate
\SynchronizedStorage\NHibernateStorageSession.cs:line 45
   at NServiceBus.Features.FeatureActivator.ActivateFeature(FeatureInfo featureI
nfo, List`1 featuresToActivate, IConfigureComponents container, PipelineSettings
 pipelineSettings) in C:\Build\src\NServiceBus.Core\Features\FeatureActivator.cs
:line 194
   at NServiceBus.Features.FeatureActivator.SetupFeatures(IConfigureComponents c
ontainer, PipelineSettings pipelineSettings) in C:\Build\src\NServiceBus.Core\Fe
atures\FeatureActivator.cs:line 57
   at NServiceBus.InitializableEndpoint.<Initialize>d__1.MoveNext() in C:\Build\
src\NServiceBus.Core\InitializableEndpoint.cs:line 50
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at NServiceBus.GenericHost.<Start>d__1.MoveNext() in C:\BuildAgent\work\fc89e
968acb99302\src\NServiceBus.Hosting.Windows\GenericHost.cs:line 54
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at NServiceBus.Hosting.Windows.WindowsHost.Start() in C:\BuildAgent\work\fc89
e968acb99302\src\NServiceBus.Hosting.Windows\WindowsHost.cs:line 33

我关注的是这里:

In order to use NServiceBus with NHibernate you need to provide at least one connection string. You can do it via (in order of precedence):

  • specifying 'NServiceBus/Persistence/NHibernate/Saga' connection string for the Saga persister

我已经检查了 NServiceBus 代码 - 这不是 generic 消息。如果问题是缺少 Timeouts1 的配置,我确定它会谈论它而不是 Saga,因为错误消息的那部分是参数化的。

但我不想在这个特定项目中使用 Sagas。我原以为 DisableFeature<Sagas> 足以向 NServiceBus 表明我不想在这里使用 sagas,但显然不是。

那么为什么 NServiceBus 试图配置 Saga 存储,更重要的是,我如何阻止它这样做?


1当然,我预计会发生这样的错误,因为我没有做任何事情,比如添加app.config 到 class 库。因此,即使 Saga 问题得到解决,我预计这段代码仍会产生错误。只是希望我能自己解决问题。

(我想避免配置任何存储背后的基本原理是,真正要做的是升级一些现有端点,这些端点为每个功能提供专用数据库 (Timeouts/Subscriptions/Sagas),尽管我一直在推荐有一段时间我们应该合并这些数据库,因此可以为每个端点配置一个 NServiceBus/Persistence 数据库,这还没有被批准。但我不愿意添加 另一个 其中一些端点的数据库,当它们目前只使用超时时)

You can do it via (in order of precedence):

这只是意味着如果您需要设置 Saga,第一点将优先于另一点,否则只需执行其他点之一:

  • specifying 'NServiceBus/Persistence' connection string that applies to all persisters
  • specifying 'NServiceBus/Persistence/connection.connection_string' or 'NServiceBus/Persistence/connection.connection_string_name' value in AppSettings or your NHibernate configuration file.

For most scenarios the 'NServiceBus/Persistence' connection string is the best option.

所以你不需要设置 Saga,但看起来你不会避免在某处设置连接字符串。

我必须承认我对 NServiceBus 的了解不多,我只是觉得有义务在他们提问时帮助那些声望很高的人:)所以我没有解释为什么它会这样工作,只有在哪里它失败。在堆栈跟踪中,您可以看到 NHibernateStorageSession.Setup,而 NHibernateStorageSession 是特征之一。因此在尝试激活该功能时失败。 Setup 像这样开始:

protected override void Setup(FeatureConfigurationContext context)
{
  NHibernateConfiguration config = new NHibernateConfigurationBuilder(context.Settings, "Saga", new string[1]
  {
    "StorageConfiguration"
  }).Build();
  // the rest
}

其中第二个参数是连接字符串后缀。在这种情况下,它被硬编码为 "Saga",并且如您所见 - 没有检查 Sagas 功能是否启用。 NHibernateConfigurationBuilder 然后尝试查找 saga 的连接字符串(或任何更通用的连接字符串),但失败并出现您观察到的异常消息。

非常简单的解决方案是通过

禁用该功能
endpointConfiguration.DisableFeature<NHibernateStorageSessio‌​n>();

但是,您当然应该注意并确保您需要的功能不依赖于该功能。我不是很清楚这个功能是干什么用的,我也没有找到它的任何文档(当然它的名字告诉我们一些关于它的用途)。

我是 Dennis van der Stelt,是 NServiceBus 制造商 Particular Software 的一名开发人员。我已经回复了你的支持案例,但也想更新这个问题,以便其他人也能找到它。 evk已经提到了大部分,我只是想提一下GH问题和完整代码。

以下代码配置确保它不会抱怨传奇连接字符串。

    endpointConfiguration.DisableFeature<NHibernateStorageSession>();
    endpointConfiguration.DisableFeature<MessageDrivenSubscriptions>();
    endpointConfiguration.DisableFeature<Sagas>();
    endpointConfiguration.UsePersistence<NHibernatePersistence, StorageType.Timeouts>();

然后只需为超时提供一个连接字符串

<add name="NServiceBus/Persistence/NHibernate/Timeout" connectionString="server=.\sqlexpress;database=nservicebus; Trusted_Connection=True;" />

这将使它按预期正常工作。

之所以需要禁用 NHibernateStorageSession,是因为它是启用的持久器中的一项功能。更多信息可以在我根据这个案例创建的 GitHub 问题中找到,which can be found here