Prerequisites
Ensure an instance of MongoDB is running on localhost:27017
. See Install MongoDB on Windows.
MongoDB management UI
To visualize the data in MongoDB, it is useful to install a MongoDB administration tool. The screen shots shown in this sample use Robomongo.
Code walk-through
This sample shows a simple client/server scenario:
Client
sends aStartOrder
message toServer
Server
starts anOrderSaga
.OrderSaga
requests a timeout with aCompleteOrder
data.- When the
CompleteOrder
timeout fires theOrderSaga
publishes anOrderCompleted
event. Server
then publishes a message that the client subscribes to.Client
handles theOrderCompleted
event.
MongoDB configuration
The Server
endpoint is configured to use the MongoDB persistence.
var endpointConfiguration = new EndpointConfiguration("Samples.MongoDB.Server");
var persistence = endpointConfiguration.UsePersistence<MongoPersistence>();
persistence.DatabaseName("Samples_MongoDB_Server");
Unless the mongo url is configured, the persistence will use the default connection string of mongodb:/
.
Similarly, unless the database name setting is configured, the persistence will use the endpoint name as the database name. In this sample the database name will be Samples_MongoDB_Server
.
Order saga
public class OrderSaga :
Saga<OrderSagaData>,
IAmStartedByMessages<StartOrder>,
IHandleTimeouts<CompleteOrder>
{
static ILog log = LogManager.GetLogger<OrderSaga>();
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<OrderSagaData> mapper)
{
mapper.ConfigureMapping<StartOrder>(message => message.OrderId)
.ToSaga(sagaData => sagaData.OrderId);
}
public Task Handle(StartOrder message, IMessageHandlerContext context)
{
Data.OrderId = message.OrderId;
var orderDescription = $"The saga for order {message.OrderId}";
Data.OrderDescription = orderDescription;
log.Info($"Received StartOrder message {Data.OrderId}. Starting Saga");
log.Info("Order will complete in 5 seconds");
var timeoutData = new CompleteOrder
{
OrderDescription = orderDescription
};
return RequestTimeout(context, TimeSpan.FromSeconds(5), timeoutData);
}
public Task Timeout(CompleteOrder state, IMessageHandlerContext context)
{
log.Info($"Saga with OrderId {Data.OrderId} completed");
var orderCompleted = new OrderCompleted
{
OrderId = Data.OrderId
};
MarkAsComplete();
return context.Publish(orderCompleted);
}
}
Saga data
The saga data is stored in the ordersagadata
collection.
public class OrderSagaData : ContainSagaData
{
public Guid OrderId { get; set; }
public string OrderDescription { get; set; }
}
IContainSagaData.
maps to the native MongoDB documentId _id
IContainSagaData.
andOriginator IContainSagaData.
both map to simple property pairs.OriginalMessageId - Custom properties on the SagaData, in this case
OrderDescription
andOrderId
, are also mapped to simple properties. _t
is type serialization metadata used by the underlying MongoDB Driver._version
is added and managed byNServiceBus.
to prevent concurrency issues.Storage. MongoDB