Using RavenDB with Azure websites

Monday 10 December 2012

In a previous post I explored various options of hosting RavenDB. While using RavenDB as a Windows Service or hosted in IIS is probably the best way to go in most cases there are a number of cases where the embedded option is great. And one of the places where I really like the embedded option of hosting RavenDB is when doing simple standalone websites. And with the new Azure website option that is a great way to host them.


The code is real straightforward and just like before. I am using a DataDirectory option when creating the EmbeddableDocumentStore here but that does the same thing as I did before using the connection string.

   1: public static DocumentStore DocumentStore { get; private set; }
   3: private void CreateRavenDB()
   4: {
   5:     DocumentStore = new EmbeddableDocumentStore()
   6:     {
   7:         DataDirectory = @"~\App_Data\db"
   8:     };
   9:     DocumentStore.Initialize();
  10: }


RavenDB Configuration

The example code from my previous blog posts will just work except for one gotcha. By default RavenDB will search for a post to use by itself. However that means scanning the available posts and that isn’t permitted by default. The result is an exception:

System.Net.NetworkInformation.NetworkInformationException: Access is denied


With a stack trace like this:

[NetworkInformationException (0x5): Access is denied]
   System.Net.NetworkInformation.SystemIPGlobalProperties.GetAllTcpConnections() +1570717
   System.Net.NetworkInformation.SystemIPGlobalProperties.GetActiveTcpListeners() +74
   Raven.Database.Util.PortUtil.FindPort() +79
   Raven.Database.Util.PortUtil.GetPort(String portStr) +133
   Raven.Database.Config.InMemoryRavenConfiguration.Initialize() +4355
   Raven.Database.Config.RavenConfiguration.LoadConfigurationAndInitialize(IEnumerable`1 values) +307
   Raven.Database.Config.RavenConfiguration..ctor() +196
   Raven.Client.Embedded.EmbeddableDocumentStore.get_Configuration() +87
   Raven.Client.Embedded.EmbeddableDocumentStore.set_DataDirectory(String value) +40
   MvcApplication3.MvcApplication.CreateRavenDB() +57
   MvcApplication3.MvcApplication.Application_Start() +169


The fix is simple. Instead of letting RavenDB search for a port by itself just specify one in the web.config. The setting required is "Raven/Port".

   1: <configuration>
   2:   <appSettings>
   3:     <add key="Raven/Port"
   4:          value="9090"/>
   5:     ....
   6:   </appSettings>
   7: ....

That is all there is to it :-)


Want to see for yourself?

I have a simple page using RavenDB as the backend store hosted on Azure right here.