NOTE: This repository is no longer maintained. If you're looking for an officially supported MongoDB persistence, please use NServiceBus.Storage.MongoDB. For documentation and samples, refer to: https://docs.particular.net/persistence/mongodb/
This package includes MongoDB persistence implementations for NServiceBus v6:
- Timeouts
- Subscriptions
- Sagas
- DataBus
Add the NServiceBus.Persistence.MongoDb
package to your NServiceBus service host project.
Install-Package NServiceBus.Persistence.MongoDb
1 Set the EndpointConfiguration
object to use MongoDbPersistence
using NServiceBus;
using NServiceBus.Persistence.MongoDB;
namespace Example
{
public class EndpointConfig : IConfigureThisEndpoint
{
public void Customize(EndpointConfiguration configuration)
{
configuration.UsePersistence<MongoDbPersistence>();
}
}
}
2 Add your MongoDB connection string to your app.config
:
<connectionStrings>
<add name="NServiceBus/Persistence/MongoDB"
connectionString="mongodb://localhost/databaseName"/>
</connectionStrings>
3 Hit F5. Yes, it is that simple.
The persistence configuration model provides a reach API. This enables to override the default
connection string name by calling .SetConnectionStringName(string)
extension method.
config
.UsePersistence<MongoDbPersistence>()
.SetConnectionStringName("SharedConnectionString");
If you are resolving your configuration setting from a different source at run-time which is
very common in cloud based deployments. Then you can use .SetConnectionString(string)
to provide it.
config
.UsePersistence<MongoDbPersistence>()
.SetConnectionString("mongodb://localhost/databaseName");
In order to get Sagas working correctly you need to enforce following
- your saga state should implement
IContainSagaData
- requires a property
Version
decorated with attribute[DocumentVersion]
Here is an example
public class OrderBillingSagaData : IContainSagaData
{
public string OrderId { get; set; }
[DocumentVersion]
public int Version { get; set; }
public bool Canceled { get; set; }
public Guid Id { get; set; }
public string Originator { get; set; }
public string OriginalMessageId { get; set; }
}
The key concurrency safeguards that sagas guarantee depend heavily on the underlying data store.
The two specific cases that NServiceBus relies on the underling data store are concurrent access to
non-existing saga instances and concurrent access to existing saga instances.
Here is how we deal with them
Concurrent access to non-existing saga instances. The persister uses MongoDb's Unique Indexes
to ensure only one document can contain the unique data.
Concurrent access to existing saga instances. The persister uses a document versioning
scheme built on top of MongoDb's findAndModify command to atomically update the existing
persisted data only if it has not been changed since it was retrieved. Since the update
is atomic, it will ensure that if there are multiple simultaneous updates to a saga, only one
will succeed.
Do you use DataBus? We also supply an implimentation that is backed with MongoDB's GridFS. To configure, just add this line to your busConfiguration:
using NServiceBus;
using NServiceBus.Persistence.MongoDB;
namespace Example
{
public class EndpointConfig : IConfigureThisEndpoint
{
public void Customize(EndpointConfiguration configuration)
{
configuration.UsePersistence<MongoDbPersistence>();
configuration.UseDataBus<MongoDbDataBus>(); //add this line!
}
}
}