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:/ 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
StartOrdermessage toServer. - Receives and handles the
OrderCompletedevent.
Server
- Receives the
StartOrdermessage and starts anOrderSaga. OrderSagasends aShipOrdermessage to the endpoint itself and requests aCompleteOrdertimeout that contains the order description.OrderSagapublishes anOrderCompletedevent and completes when theCompleteOrdertimeout 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.. 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);
}
}