Connection Options

Component: RavenDB Persistence
NuGet Package NServiceBus.RavenDB (2.x)
Target NServiceBus Version: 5.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.

When the RavenDB DocumentStore is created by the user at endpoint configuration time it's important to dispose it, by calling the Dispose() method, before shutting down the endpoint process.

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 = busConfiguration.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 = busConfiguration.UsePersistence<RavenDBPersistence>();
persistence.UseDocumentStoreForSubscriptions(documentStore);
persistence.UseDocumentStoreForSagas(documentStore);
persistence.UseDocumentStoreForTimeouts(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 = busConfiguration.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