Run the Azure worker role in elevated mode to register HttpListener

If you try to run RavenDB in Azure worker role (, it might fail with the following error. (You will need to attach a debugger to WaWorkerHost process to see this error message. Since its an handled exception, you do not see it in event logs)

CLR exception type: System.Net.HttpListenerException "Access is denied"
Error: StartRaven Error: System.Net.HttpListenerException (0x80004005): Access is denied
   at System.Net.HttpListener.AddAllPrefixes()
   at System.Net.HttpListener.Start()
   at Raven.Database.Server.HttpServer.StartListening()
   at RavenDbWorkerRole.WorkerRole.StartRaven(RavenConfiguration config)
Microsoft.WindowsAzure.ServiceRuntime Information: 202 : Role entrypoint . COMPLETED OnStart()
CLR exception - code e0434352 (first chance)
Microsoft.WindowsAzure.ServiceRuntime Information: 203 : Role entrypoint . CALLING   Run()
Information: RavenDbWorkerRole entry point called
Information: Working

RavenDB registers HTTPListener. To register HttpListener you need admin previledges. However, worker role by default does not have admin previledges.
In order to run the wroker role with admin previledges, modify the csdef file and add the following:
<Runtime executionContext ="elevated" />
This makes the worker role run under elevated permission and hence it is able to register httplistener.

Sample csdef file:
< ?xml version="1.0" encoding="utf-8"? >
< ServiceDefinition name="WindowsAzureProject6"xmlns="" >
  < WorkerRole name="WorkerRole1" vmsize="Small" >
    < Runtime executionContext ="elevated" />
    < Imports >
      < Import moduleName="Diagnostics" />
    < /Imports >
   < /WorkerRole >
< /ServiceDefinition >

The key take away is that if you want to register HttpListener, run the worker role in elevated permission.

Daniel Roth has explained why you need admin previledges to register HttpListener at
The HttpListener is built on top of the HTTP APIs, also known as HTTP.SYS.  Before an application can register to receive requests for a URL namespace, the system administrator must make a reservation for that URL on behalf of the user running the application. By default, the namespace is closed, that is, only the administrator can register UrlPrefixes until the administrator enters a reservation.
For more information please refer to this MSDN article on how to reserve namespaces for users: