AI agents: use the documentation index at llms.txt to locate machine-readable pages. This section is indexed by https://docs.particular.net/samples/llms.txt. The markdown version of this page is served as plain text. An MCP server at /mcp serves the same content via the search_docs and read_doc tools; it is read-only and needs no credentials. Markdown versions of documentation pages are available by appending .md to the page URL. Directory URLs use index.md. They are served as text/plain because some retrieval backends reject text/markdown.

Simple DynamoDB Persistence Usage

NuGet Package:
NServiceBus.Persistence.DynamoDB 4.x
Target Version:
NServiceBus 10.x

This sample shows a client/server scenario in which the server uses DynamoDB persistence to store saga data.

Prerequisites

This sample uses a DynamoDB local instance at http://localhost:8000 by default. See the AWS guidance on deploying DynamoDB local.

Alternatively, with Docker installed locally, run the following command in the solution directory:

docker compose up -d

The container keeps the data in memory only, so the data is lost when the container is removed.

Projects

Client

  • Sends the StartOrder message to Server.
  • Receives and handles the OrderCompleted event.

Server

  • Receives the StartOrder message and starts an OrderSaga.
  • OrderSaga sends a ShipOrder message to the endpoint itself and requests a CompleteOrder timeout that contains the order description.
  • OrderSaga publishes an OrderCompleted event and completes when the CompleteOrder timeout fires.

SharedMessages

Contains the shared message contracts used by all endpoints.

Persistence config

The Server endpoint uses DynamoDB persistence and stores saga data in a shared table named Samples.DynamoDB.Simple. With installers enabled, the endpoint creates the table at startup if it does not exist. The AmazonDynamoDBClient connects to DynamoDB local with placeholder credentials.

var amazonDynamoDbClient = new AmazonDynamoDBClient(
    new BasicAWSCredentials("localdb", "localdb"),
    new AmazonDynamoDBConfig
    {
        ServiceURL = "http://localhost:8000"
    });

var endpointConfiguration = new EndpointConfiguration("Samples.DynamoDB.Simple.Server");

var persistence = endpointConfiguration.UsePersistence<DynamoPersistence>();
persistence.DynamoClient(amazonDynamoDbClient);
persistence.UseSharedTable(new TableConfiguration
{
    TableName = "Samples.DynamoDB.Simple"
});

Order saga data

public class OrderSagaData :
    ContainSagaData
{
    public Guid OrderId { get; set; }
    public string? OrderDescription { get; set; }
}

Order saga

public class OrderSaga(ILogger<OrderSaga> logger) :
    Saga<OrderSagaData>,
    IAmStartedByMessages<StartOrder>,
    IHandleTimeouts<CompleteOrder>
{
    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<OrderSagaData> mapper)
    {
        mapper.MapSaga(saga => saga.OrderId).ToMessage<StartOrder>(msg => msg.OrderId);
    }

    public Task Handle(StartOrder message, IMessageHandlerContext context)
    {
        var orderDescription = $"The saga for order {message.OrderId}";
        Data.OrderDescription = orderDescription;

        logger.LogInformation("Received StartOrder message {OrderId}. Starting Saga", Data.OrderId);

        var shipOrder = new ShipOrder
        {
            OrderId = message.OrderId
        };

        logger.LogInformation("Order will complete in 5 seconds");
        var timeoutData = new CompleteOrder
        {
            OrderDescription = orderDescription,
        };

        return Task.WhenAll(
            context.SendLocal(shipOrder),
            RequestTimeout(context, TimeSpan.FromSeconds(5), timeoutData)
        );
    }

    public Task Timeout(CompleteOrder state, IMessageHandlerContext context)
    {
        logger.LogInformation("Saga with OrderId {OrderId} completed", Data.OrderId);
        MarkAsComplete();
        var orderCompleted = new OrderCompleted
        {
            OrderId = Data.OrderId
        };
        return context.Publish(orderCompleted);
    }
}

Related Articles

  • AWS DynamoDB persistence
    How to use NServiceBus with AWS DynamoDB.
  • Sagas
    Master NServiceBus sagas to coordinate distributed workflows and ensure reliable long-running processes.