我已经设置了一个事件(以及在传奇中使用的几个命令)。我有2个Web应用程序和1个控制台应用程序,用于注册端点。所有用户都关闭了自动订阅。一个应用程序为该事件声明一个处理程序,然后手动订阅该事件。
当我在容器中运行应用程序并使用sqlPersistence和RabbitMq传输时,一次又一次调用处理程序。当我在没有容器的情况下运行并且没有显式设置任何持久性并使用LearningTransport时,代码将按预期运行。
我确实知道处理程序将在发生异常时一遍又一遍地被调用,但这取决于默认的重试次数(5?),并且处理程序中没有异常发生,因此我认为这就是问题所在。
从网络应用发布事件(向其处理程序发送命令时出现相同的问题)时,针对同一条消息反复调用该处理程序:消息ID,关联ID,对话ID,发送时间, ...一切都是一样的,我不知道为什么,也找不到互联网上的其他热门歌曲。
我使用autofac作为容器,使用NSB 7.0.1和RabbitMq进行传输。所有应用程序和持久性(SQL Server)和传输都在Docker容器中运行。
对于每个应用程序,我都设置了相同的设置,只是我仅在一个应用程序中注册了处理程序,如下所示:
感谢您的帮助!
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{
//...
var loggerFactory =
container.Resolve<Microsoft.Extensions.Logging.ILoggerFactory>();
var logFactory = LogManager.Use<MicrosoftLogFactory>();
logFactory.UseMsFactory(loggerFactory);
var endpointConfiguration = new EndpointConfiguration(applicationName);
endpointConfiguration.EnableInstallers();
endpointConfiguration.SendFailedMessagesTo("error");
endpointConfiguration.AuditProcessedMessagesTo("audit");
endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
endpointConfiguration.DisableFeature<AutoSubscribe>();
EnsureSqlDatabaseExists(persistenceConnectionString, container.Resolve<ILogger<IEndpointInstance>>());
var persistence = endpointConfiguration.UsePersistence<SqlPersistence>();
persistence.TablePrefix("Monrovo");
var dialect = persistence.SqlDialect<SqlDialect.MsSqlServer>();
dialect.Schema("receiver");
persistence.ConnectionBuilder(() => new SqlConnection(persistenceConnectionString));
var transport = endpointConfiguration.UseTransport<RabbitMQTransport>();
transport.UseConventionalRoutingTopology();
transport.ConnectionString(transportConnectionString);
endpointConfiguration.UseContainer<AutofacBuilder>(customizations =>
{
customizations.ExistingLifetimeScope(container);
});
await ScriptRunner.Install(
sqlDialect: new SqlDialect.MsSqlServer(),
tablePrefix: "Monrovo",
connectionBuilder: () => new SqlConnection(persistenceConnectionString),
scriptDirectory: Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"/NServiceBus.Persistence.Sql/MsSqlServer",
shouldInstallOutbox: true,
shouldInstallSagas: true,
shouldInstallSubscriptions: true,
shouldInstallTimeouts: true);
var startableEndpoint = await Endpoint.Create(endpointConfiguration);
var endpoint = await startableEndpoint.Start();
_endpointInstance.Subscribe<ArticleApproved>().Wait();
var updateBuilder = new ContainerBuilder();
updateBuilder.RegisterInstance(_endpointInstance)
.As<IMessageSession>().As<IEndpointInstance>().SingleInstance();
// There is currently no workaround from NServiceBus for this.
// See https://github.com/Particular/NServiceBus/issues/4421
#pragma warning disable CS0618 // Type or member is obsolete
updateBuilder.Update(container);
#pragma warning restore CS0618 // Type or member is obsolete
return new AutofacServiceProvider(container);
}
private static void EnsureSqlDatabaseExists(string connectionString, ILogger<IEndpointInstance> logger)
{
var builder = new SqlConnectionStringBuilder(connectionString);
var originalCatalog = builder.InitialCatalog;
builder.InitialCatalog = "master";
var masterConnectionString = builder.ConnectionString;
try
{
using (var connection = new SqlConnection(masterConnectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
$"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '{originalCatalog}')" +
$" CREATE DATABASE [{originalCatalog}]; ";
command.ExecuteNonQuery();
}
}
catch (SqlException)
{
// Need to handle this better. Locally, we need to use master first. In Azure this is not possible or necessary
// TODO: Build a sql docker container with a database already created.
builder.InitialCatalog = originalCatalog;
masterConnectionString = builder.ConnectionString;
try
{
using (var connection = new SqlConnection(masterConnectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
$"IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = '{originalCatalog}')" +
$" CREATE DATABASE [{originalCatalog}]; ";
command.ExecuteNonQuery();
}
}
catch (SqlException innerException)
{
logger.LogCritical(innerException, $"Unable to connect to SQL Server. Check that {builder.DataSource} is available");
}
}
builder.InitialCatalog = originalCatalog;
masterConnectionString = builder.ConnectionString;
try
{
using (var connection = new SqlConnection(masterConnectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
$"IF NOT EXISTS(SELECT 1 FROM sys.schemas WHERE name = 'receiver')" +
$"BEGIN EXEC sys.sp_executesql N'CREATE SCHEMA receiver;' END";
command.ExecuteNonQuery();
}
}
catch { }
}
public class test : IHandleMessages<ArticleApproved>
{
public Task Handle(RfpApproved message, IMessageHandlerContext context)
{
// Initially handles the published event and starts the saga
context.SendLocal(new StartRFPSync(message.RfpId));
return Task.CompletedTask;
}
}
答案 0 :(得分:0)
问题最终是在断点处停止时引发异常(我认为是超时)。 NSB心跳设置为5秒,因此延长开发时间可以解决该问题,但是我已经确认了让代码运行时我的问题不存在。通常,调试代码是件好事,但异步进程会导致