Using RavenDB in NServiceBus – Connecting

In a previous post I talked about the different ways to make sure that you have a Raven server available in order for your NServiceBus endpoints to store their data. In this post we’ll look at the different options for connecting to that Raven server.

To tell NServiceBus to use Raven for persistence is a easy as calling Configure.RavenPersistence(). This is the default configuration and it uses the following conventions:

  1. If no master node is configured it will assume that a Raven server is running at http://localhost:8080, the default url for RavenDB.
  2. If a master node is configured the url used will be: http://{masternode}/:8080
  3. If a connection string named “NServiceBus.Persistence” is found the value of the connectionString attribute will be used. This pretty much give you full control over which Raven server your endpoint will use
If NServiceBus detects that any of the Raven related storage’s is used, Saga, Subscription, Timeouts, etc the above will be automatically configured for you. So in essense there should be no need for you to explicitly configure Raven unless you need to override the defaults.

Overriding the defaults

There are a few situations where the default behavior might not be good enough for you.

  • You want to use your own connection string – If you’re using Raven for your own data as well you might want to share connection string. You do this by using the Configure.RavenPersistence(string connectionString) signature. This tells NServiceBus to connection to the server specified in that string. The default connection string for RavenDB is “RavenDB”.
  • You want to specify a explicit database name – If you want to control the database name in code instead of configuration you would use the  Configure.RavenPersistence(string connectionString, string databaseName) signature. This could be useful in multi tenant scenarios

What database will be used?

After connecting to a Raven server we need to decide which actual database to use. Unless we find a default database specified in the connection string NServiceBus will use the endpoint name as the database name. So if your endpoint is named “MyServer” the database name will be “MyServer”. Each endpoint will have a separate database unless you explicitly override using the connection string. RavenDB will automatically create the database if it doesn’t already exist.

A detailed explanation of the endpoint name concept can be found here.

Can I use the IDocumentStore used by NServiceBus for my own data?

No the Raven client is merged and internalized into the NServiceBus assemblies so if you want to use Raven for your own purposes you need to reference the Raven client and setup your own document store.

How do I look at the data?

Just open a web browser and type the url if the Raven server, this will open up the Raven DB management studio.

This entry was posted in NServiceBus, RavenDB. Bookmark the permalink. Both comments and trackbacks are currently closed.
  • Johannes Gustafsson

    If I register a document store using the Castle builder, I get the following exception: ”Component IDocumentStore could not be registered. There is already a component with that name. Did you want to modify the existing component instead? If not, make sure you specify a unique name.”

    Seems to work with autofac and structuremap though.

    • Andreas Öhlund

      That sounds like a bug in the castle builder (or castle it self). I’ll take a look!

    • Andreas Öhlund

      Are you using ConfigureComponent or RegisterSingleton btw?

  • darth jit

    so if there is a logical publisher made up of 3 physical publishers(hosted in 3 separate  servers), where does raven live? Since the 3 publishers need to share subscription storage, am I correct in assuming that one of the 3 servers will host ravenDB?  

    • andreasohlund

      Yes either a central raven or 3 ravens with replication setup.

  • Mihir Mehta


    We’re using NServiceBus version Say I already have RavenDB installed on my machine…but its listening on a different port (not 8080). In that case…how do i make NServiceBus point to my instance of Raven…..which is listening on port 8090?

    And when you say – “The default connection string for RavenDB is “RavenDB”.” does it mean…that i must have a config file entry with a connectionstring name as “RavenDB”?