Connection Options

Component: RavenDB Persistence
NuGet Package NServiceBus.RavenDB (4.x)
Target NServiceBus Version: 6.x

The following sections outline various ways to connect to the RavenDB server. Specifying an external shared store (providing a fully configured RavenDB DocumentStore instance) is preferred so that the RavenDB DTC settings can be configured for optimal data safety.

External shared store at initialization

To use an external DocumentStore, but defer its creation until NServiceBus initializes, a Func<ReadOnlySettings, IDocumentStore> can be provided which will allow the DocumentStore to be created with access to the ReadOnlySettings. This gives the ability to configure the document store based on conventions derived from endpoint data present in the settings object. For example, the DocumentStore instance can be configured to use the Endpoint Name as its database name by accessing readOnlySettings.EndpointName().

var persistence = endpointConfiguration.UsePersistence<RavenDBPersistence>();
persistence.SetDefaultDocumentStore(readOnlySettings =>
{
    var documentStore = new DocumentStore();
    // configure documentStore here
    return documentStore;
});

External store at initialization for a specific persister

A DocumentStore can be created at initialization time, with access to endpoint settings found in ReadOnlySettings, for usage in a specific persister (e.g. timeouts) by using the following code:

var persistence = endpointConfiguration.UsePersistence<RavenDBPersistence>();
persistence.UseDocumentStoreForSubscriptions(readOnlySettings =>
{
    var documentStore = new DocumentStore();
    // configure documentStore here
    return documentStore;
});
persistence.UseDocumentStoreForSagas(readOnlySettings =>
{
    var documentStore = new DocumentStore();
    // configure documentStore here
    return documentStore;
});
persistence.UseDocumentStoreForTimeouts(readOnlySettings =>
{
    var documentStore = new DocumentStore();
    // configure documentStore here
    return documentStore;
});
persistence.UseDocumentStoreForGatewayDeduplication(readOnlySettings =>
{
    var documentStore = new DocumentStore();
    // configure documentStore here
    return documentStore;
});

External shared store for all persisters

The RavenDB DocumentStore instance can be provided directly to NServiceBus to use for all persisters. This enables sharing the same application database for NServiceBus data as well.

var documentStore = new DocumentStore();
// configure documentStore here
var persistence = endpointConfiguration.UsePersistence<RavenDBPersistence>();
persistence.SetDefaultDocumentStore(documentStore);

External store for a specific persister

An externally created DocumentStore instance can be used for a specific persister (e.g. timeouts) by using the following code:

var documentStore = new DocumentStore();
// configure documentStore here
var persistence = endpointConfiguration.UsePersistence<RavenDBPersistence>();
persistence.UseDocumentStoreForSubscriptions(documentStore);
persistence.UseDocumentStoreForSagas(documentStore);
persistence.UseDocumentStoreForGatewayDeduplication(documentStore);

Shared store for all persisters defined via connection string

Instead of connecting to a database on the local server, a connection string can be provided via configuration. The runtime will look for a connection string named NServiceBus/Persistence/RavenDB or NServiceBus/Persistence.

<configuration>
  <connectionStrings>
    <add name="NServiceBus/Persistence/RavenDB" 
         connectionString="Url = http://localhost:9090"/>
    <add name="NServiceBus/Persistence" 
         connectionString="Url = http://localhost:9090"/>
  </connectionStrings>
</configuration>

RavenDB connection strings can include the database name and other parameters as well. See How to set up a connection string in the RavenDB documentation for more details. The runtime will use the connection string to create a shared DocumentStore instance for all persisters.

Shared store for all persisters defined via connection parameters

Rather than specifying connection information in configuration, connection details can be established via an instance of ConnectionParameters that allows specifying Url, DatabaseName and the ApiKey for the RavenDB instance for usage in all the persisters. The runtime will use the parameters to create a shared DocumentStore instance for all persisters.

var connectionParams = new ConnectionParameters();
// configure connection params (ApiKey, DatabaseName, Url) here
var persistence = endpointConfiguration.UsePersistence<RavenDBPersistence>();
persistence.SetDefaultDocumentStore(connectionParams);

Store defined via a connection string for a specific persister

One can configure a RavenDB connection string that is only applicable to a specific store:

<configuration>
  <connectionStrings>
    <add name="NServiceBus/Persistence/RavenDB/Subscription" 
         connectionString="Url = http://localhost:9090"/>
    <add name="NServiceBus/Persistence/RavenDB/Saga" 
         connectionString="Url = http://localhost:9090"/>
    <add name="NServiceBus/Persistence/RavenDB/Timeout" 
         connectionString="Url = http://localhost:9090"/>
    <add name="NServiceBus/Persistence/RavenDB/Outbox" 
         connectionString="Url = http://localhost:9090"/>
    <add name="NServiceBus/Persistence/RavenDB/GatewayDeduplication" 
         connectionString="Url = http://localhost:9090"/>
  </connectionStrings>
</configuration>

Default

By default, a DocumentStore is created that connects to http://localhost:8080 and uses the endpoint name as its database name. This default connection is used for all the persisters.

Database used

After connecting to a RavenDB server, decide which actual database to use. Unless NServiceBus finds a default database specified in the connection string, NServiceBus uses the endpoint name as the database name. So if the endpoint is named MyServer, the database name is MyServer. Each endpoint has a separate database unless explicitly overridden via the connection string. RavenDB automatically creates the database if it doesn't already exist.

See also How to specify endpoint name.

Samples


Last modified