This package allows sagas to be stored in MongoDB. This means that you can orchestrate a long-running process and manage its state throughout execution.
MassTransit.Persistence.MongoDb can be installed via the package manager console by executing the following commandlet:
PM> Install-Package MassTransit.MongoDbIntegration
Once we have the package installed, we can create a MongoDbSagaRepository
using one of the following constructors:
var repository = new MongoDbSagaRepository(new MongoUrl("mongodb://localhost/masstransitTest"));
Or
var repository = new MongoDbSagaRepository("mongodb://localhost", "masstransitTest"));
Say we have an InitiateSimpleSaga
message:
class InitiateSimpleSaga :
CorrelatedBy<Guid>
{
public InitiateSimpleSaga(Guid correlationId)
{
CorrelationId = correlationId;
}
public Guid CorrelationId { get; }
}
And we have a SimpleSaga
that is initiated by the InitiateSimpleSaga
message:
class SimpleSaga :
InitiatedBy<InitiateSimpleSaga>,
IVersionedSaga
{
public Guid CorrelationId { get; set; }
public int Version { get; set; }
public Task Consume(ConsumeContext<InitiateSimpleSaga> context)
{
//Do some cool stuff...
Console.WriteLine($"{nameof(InitiateSimpleSaga)} consumed");
return Task.FromResult(0);
}
}
Now we have our saga and our initiation message, we can set up our bus to use the MongoDbSagaRepository
:
var busControl = Bus.Factory.CreateUsingInMemory(configurator =>
{
configurator.ReceiveEndpoint("my_awesome_endpoint", endpoint =>
{
//Normal receive endpoint config...
endpoint.Saga(new MongoDbSagaRepository<SimpleSaga>(new MongoUrl("mongodb://localhost/masstransitTest")));
});
});
With everything now configured we can raise the InitiateSimpleSaga
message to get the saga kicked off:
var id = Guid.NewGuid();
var message = new InitiateSimpleSaga(id);
await busControl.Publish(message);
- Fork
- Hack!
- Pull Request