Skip to content

Instantly share code, notes, and snippets.

@rdingwall
Created March 9, 2012 12:27
Show Gist options
  • Save rdingwall/2006319 to your computer and use it in GitHub Desktop.
Save rdingwall/2006319 to your computer and use it in GitHub Desktop.
RavenDB ASP.NET Web API session management
public class BooksApiController : FooApiController
{
public IEnumerable<Book> Get()
{
return DocumentSession
.Query<Book>()
.OrderByAscending(p => p.Author)
.Take(100);
}
}
public abstract class FooApiController : ApiController
{
public IDocumentSession DocumentSession { get; set; }
}
// or self-hosted startup
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
...
var config = GlobalConfiguration.Configuration;
config.Filters.Add(new RavenDbApiAttribute(documentStore));
}
}
public class RavenDbApiAttribute : ActionFilterAttribute
{
readonly IDocumentStore documentStore;
public RavenDbApiAttribute(IDocumentStore documentStore)
{
if (documentStore == null) throw new ArgumentNullException("documentStore");
this.documentStore = documentStore;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
var controller = actionContext.ControllerContext.Controller as FooApiController;
if (controller == null)
return;
controller.DocumentSession = documentStore.OpenSession();
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var controller = actionExecutedContext.ActionContext.ControllerContext.Controller as FooApiController;
if (controller == null)
return;
controller.DocumentSession.Dispose();
}
}
@BillKeenan
Copy link

where do you get documentStore from in application_start ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment