MongoDB persistence for NServicBus 5.x
- Get the source and build locally
or
- Install the
NServiceBus.MongoDB
NuGet package using the Visual Studio NuGet Package Manager
After adding a reference to it from your project, specify MongoDBPersistence
to be used for persistence.
using NServiceBus;
using NServiceBus.MongoDB;
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
public void Customize(BusConfiguration configuration)
{
configuration.UsePersistence<MongoDBPersistence>()
.SetDatabaseName("MyDatabase");
}
}
This base configuration connects using NServiceBus/Persistence/MongoDB
connection string in the app.config and MyDatabase
as the database.
<connectionStrings>
<add name="NServiceBus/Persistence/MongoDB" connectionString="mongodb://localhost:27017" />
</connectionStrings>
If this connection string is not found NServiceBus/Persistence
is used.
<connectionStrings>
<add name="NServiceBus/Persistence" connectionString="mongodb://localhost:27017" />
</connectionStrings>
or specify the connection string to use:
using NServiceBus;
using NServiceBus.MongoDB;
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
public void Customize(BusConfiguration configuration)
{
configuration.UsePersistence<MongoDBPersistence>()
.SetConnectionString("mongodb://localhost:27017")
.SetDatabaseName("MyDatabase");
}
}
To configure NServiceBus to use MongoDB GridFS as the persistence for DataBus use the following configuration.
using NServiceBus;
using NServiceBus.MongoDB;
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
public void Customize(BusConfiguration configuration)
{
configuration.UsePersistence<MongoDBPersistence>();
configuration.UseDataBus<MongoDBDataBus>();
}
}
Saga data needs to be defined the normal way NSB requires with the additional interface IHaveDocumentVersion
to work appropriately with NServiceBus.MongoDB
. All this interface adds is a version property. Alternatively you can just inherit from ContainMongoSagaData as follows:
using NServiceBus.MongoDB;
using NServiceBus.Saga;
public class MySagaData : ContainMongoSagaData
{
[Unique]
public string SomeId { get; set; }
public int Count { get; set; }
}
See MongoDB persistence Sample
(Special thanks to Simon Cropp
for the example.)
See Simple sample