Skip to content

Marten Integration

Marten and Wolverine are sibling projects under the JasperFx organization, and as such, have quite a bit of synergy when used together. At this point, adding the WolverineFx.Marten Nuget dependency to your application adds the capability to combine Marten and Wolverine to:

  • Simplify persistent handler coding with transactional middleware
  • Use Marten and Postgresql as a persistent inbox or outbox with Wolverine messaging
  • Support persistent sagas within Wolverine applications
  • Effectively use Wolverine and Marten together for a Decider function workflow with event sourcing
  • Selectively publish events captured by Marten through Wolverine messaging
  • Process events captured by Marten through Wolverine message handlers through either subscriptions or the older event forwarding.

Getting Started

To use the Wolverine integration with Marten, just install the Wolverine.Persistence.Marten Nuget into your application. Assuming that you've configured Marten in your application (and Wolverine itself!), you next need to add the Wolverine integration to Marten as shown in this sample application bootstrapping:

cs
var builder = WebApplication.CreateBuilder(args);
builder.Host.ApplyOaktonExtensions();

builder.Services.AddMarten(opts =>
    {
        opts.Connection(Servers.PostgresConnectionString);
        opts.DatabaseSchemaName = "chaos2";
    })
    .IntegrateWithWolverine();

builder.Host.UseWolverine(opts =>
{
    opts.Policies.OnAnyException().RetryWithCooldown(50.Milliseconds(), 100.Milliseconds(), 250.Milliseconds());
    
    opts.Services.AddScoped<IMessageRecordRepository, MartenMessageRecordRepository>();
    
    opts.Policies.DisableConventionalLocalRouting();
    opts.UseRabbitMq().AutoProvision();
    
    opts.Policies.UseDurableInboxOnAllListeners();
    opts.Policies.UseDurableOutboxOnAllSendingEndpoints();

    opts.ListenToRabbitQueue("chaos2");
    opts.PublishAllMessages().ToRabbitQueue("chaos2");
    
    
    opts.Policies.AutoApplyTransactions();
});

snippet source | anchor

cs
var builder = WebApplication.CreateBuilder(args);
builder.Host.ApplyOaktonExtensions();

builder.Services.AddMarten(opts =>
    {
        var connectionString = builder
            .Configuration
            .GetConnectionString("postgres");

        opts.Connection(connectionString);
        opts.DatabaseSchemaName = "orders";
    })
    // Optionally add Marten/Postgresql integration
    // with Wolverine's outbox
    .IntegrateWithWolverine();

// You can also place the Wolverine database objects
// into a different database schema, in this case
// named "wolverine_messages"
//.IntegrateWithWolverine("wolverine_messages");

builder.Host.UseWolverine(opts =>
{
    // I've added persistent inbox
    // behavior to the "important"
    // local queue
    opts.LocalQueue("important")
        .UseDurableInbox();
});

snippet source | anchor

For more information, see durable messaging and the sample Marten + Wolverine project.

Using the IntegrateWithWolverine() extension method behind your call to AddMarten() will:

Marten as Outbox

See the Marten as Outbox page.

Transactional Middleware

See the Transactional Middleware page.

Marten as Inbox

See the Marten as Inbox page.

Saga Storage

See the Marten as Saga Storage page.

Released under the MIT License.