Skip to content

sbmako/NServiceBus.MongoDB

Repository files navigation

NServiceBus.MongoDB Build Status NuGet Status

MongoDB persistence for NServicBus 7.x

Installation

  • Get the source and build locally

or

Configuration

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");
  }
}

DataBus Configuration

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>();
  }
}

Sagas

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; }
}

Samples

See Simple sample

Build Instructions

  • Prerequisistes
  • Run make from the root directory