Provides an implementation of Rebus.Outbox abstraction using MS SQL Server as an outbox storage.
Here's a code fragment from the sample application:
services.AddRebus(configure => configure
.Transport(x =>
{
x.UseRabbitMq(rabbitMqConnectionString, rabbitMqOptions.InputQueueName);
})
.Outbox(c => c.UseSqlServer(() =>
{
var dbConnection = (DbConnection) SqlConnectionFactory.GetOpenConnection(dataAccessOptions.ConnectionString);
return Task.FromResult(dbConnection);
}, "dbo.OutboxMessages"), options =>
{
options.RunMessagesProcessor = true;
options.MaxMessagesToRetrieve = 5;
})
.Routing(r => r.TypeBased()));
The key point is to have some method of sharing database connections between Outbox and business services. I use Rsi.LocalTransactions for that:
public static class SqlConnectionFactory
{
public static SqlConnection GetOpenConnection(string connectionString)
{
var currentScope = DbConnectionScope.Current;
if (currentScope != null)
{
return (SqlConnection) DbConnectionScope.Current.GetOpenConnection(SqlClientFactory.Instance, connectionString);
}
var sqlConnection = (SqlConnection) SqlClientFactory.Instance.CreateConnection();
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
return sqlConnection;
}
}