Sql Server Transaction Middleware Edit on GitHub

Assuming that the Jasper.Persistence.SqlServer Nuget is referenced by your project, you can use the [Transactional] attribute on message (or HTTP) handler methods to wrap the message handling inside a single Sql Server transaction like so:

public class ItemCreatedHandler
    public static async Task Handle(
        ItemCreated created,
        SqlTransaction tx, // the current transaction
        Jasper.Messaging.Tracking.MessageTracker tracker,
        Envelope envelope)
        // Using some extension method helpers inside of Jasper here
        await tx.CreateCommand("insert into receiver.item_created (id, name) values (@id, @name)")
            .With("id", created.Id)
            .With("name", created.Name)

        tracker.Record(created, envelope);

When you use this middleware, be sure to pull in the current SqlTransaction object as a parameter to your handler method.