MongoDB persistence for NServicBus 7.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
. This interface adds two additional properties, a version number and ETag property. Alternatively you can just inherit from ContainMongoSagaData as follows:
using NServiceBus.MongoDB;
using NServiceBus.Saga;
public class MySagaData : ContainMongoSagaData
{
public string SomeId { get; set; }
public int Count { get; set; }
}
See Simple sample
- Prerequisistes
- .Net Core SDK 2
- Gnu Make (or compatible)
- A running default local instance of MongoDB listening on the default port:
mongodb://localhost:27017
- Run
make
from the root directory