SDS.Outbox.NHibernate
1.2.0
dotnet add package SDS.Outbox.NHibernate --version 1.2.0
NuGet\Install-Package SDS.Outbox.NHibernate -Version 1.2.0
<PackageReference Include="SDS.Outbox.NHibernate" Version="1.2.0" />
<PackageVersion Include="SDS.Outbox.NHibernate" Version="1.2.0" />
<PackageReference Include="SDS.Outbox.NHibernate" />
paket add SDS.Outbox.NHibernate --version 1.2.0
#r "nuget: SDS.Outbox.NHibernate, 1.2.0"
#:package SDS.Outbox.NHibernate@1.2.0
#addin nuget:?package=SDS.Outbox.NHibernate&version=1.2.0
#tool nuget:?package=SDS.Outbox.NHibernate&version=1.2.0
Transactional Outbox for NHibernate Integration
This document summarizes the Transactional Outbox support added to MassTransit.NHibernateIntegration, based on the same overall approach used by MassTransit.EntityFrameworkCoreIntegration.
Goals
- Support the Inbox/Outbox pattern for NHibernate-based consumer pipelines.
- Ensure idempotent processing using
MessageId + ConsumerId. - Store
Send/Publishoperations in the outbox within the same transaction as message consumption. - Periodically clean up delivered inbox records after the duplicate detection window expires.
Added Files
Outbox Configuration
Configuration/INHibernateOutboxConfigurator.csConfiguration/INHibernateBusOutboxConfigurator.csConfiguration/Configuration/NHibernateOutboxConfigurator.csConfiguration/Configuration/NHibernateBusOutboxConfigurator.csConfiguration/NHibernateOutboxConfigurationExtensions.cs
Outbox Runtime
NHibernateIntegration/Outbox/NHibernateOutboxOptions.csNHibernateIntegration/Outbox/NHibernateOutboxContextFactory.csNHibernateIntegration/Outbox/NHibernateOutboxConsumeContext.csNHibernateIntegration/Outbox/NHibernateScopedBusContext.csNHibernateIntegration/Outbox/NHibernateConsumeContextScopedBusContext.csNHibernateIntegration/Outbox/NHibernateScopedBusContextProvider.csNHibernateIntegration/Outbox/BusOutboxDeliveryService.csNHibernateIntegration/Outbox/InboxCleanupService.csNHibernateIntegration/Outbox/NHibernateOutboxExtensions.cs
Outbox State Model and Mappings
NHibernateIntegration/Outbox/InboxState.csNHibernateIntegration/Outbox/OutboxState.csNHibernateIntegration/Outbox/OutboxMessage.csNHibernateIntegration/Outbox/InboxStateMap.csNHibernateIntegration/Outbox/OutboxStateMap.csNHibernateIntegration/Outbox/OutboxMessageMap.csNHibernateIntegration/Outbox/NHibernateOutboxMappings.cs
New APIs
Register Outbox in DI
x.AddNHibernateOutbox(o =>
{
o.IsolationLevel = System.Data.IsolationLevel.RepeatableRead;
// o.DisableInboxCleanupService();
o.UseBusOutbox();
});
Apply Transactional Outbox on a Receive Endpoint
e.UseNHibernateOutbox(context);
Legacy overload is also available:
e.UseNHibernateOutbox(provider);
Full Usage
1) Add Outbox Mappings to NHibernate Model
When creating ISessionFactory, include outbox mapping types:
var mappedTypes = new[]
{
// ... your saga mappings
}.Concat(MassTransit.NHibernateIntegration.Outbox.NHibernateOutboxMappings.Mappings);
Or pass NHibernateOutboxMappings.Mappings directly to your session factory provider.
2) Register ISessionFactory in DI
Ensure ISessionFactory is registered in the container so the outbox context factory can be resolved.
3) Configure MassTransit
services.AddMassTransit(x =>
{
x.AddNHibernateOutbox(o =>
{
o.IsolationLevel = System.Data.IsolationLevel.RepeatableRead;
o.DuplicateDetectionWindow = TimeSpan.FromMinutes(30);
o.QueryDelay = TimeSpan.FromSeconds(10);
o.QueryMessageLimit = 100;
o.QueryTimeout = TimeSpan.FromSeconds(30);
});
x.UsingRabbitMq((context, cfg) =>
{
cfg.ReceiveEndpoint("example-queue", e =>
{
e.UseNHibernateOutbox(context);
e.ConfigureConsumers(context);
});
});
});
Important Notes
- This implementation focuses on consumer transactional outbox (inbox + outbox in the consume pipeline).
- It includes bus outbox support via
UseBusOutbox(...), which interceptsIPublishEndpointandISendEndpointProviderand delivers persisted outbox messages using a hosted background service. - Duplicate detection locking uses pessimistic row locking (
LockMode.Upgrade) onInboxState. - You should add appropriate indexes/constraints in your database schema for performance and duplicate prevention.
Verification
The changes were validated with successful builds of:
MassTransit.NHibernateIntegration.csprojMassTransit.sln
Transactional Outbox for NHibernate Integration
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Iesi.Collections (>= 4.0.5)
- MassTransit (>= 1.0.0)
- NHibernate (>= 5.4.2)
-
.NETStandard 2.0
- Iesi.Collections (>= 4.0.5)
- MassTransit (>= 1.0.0)
- NHibernate (>= 5.4.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.