-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRavenController.cs
55 lines (49 loc) · 1.76 KB
/
RavenController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.AspNet.Identity.Owin;
using Raven.Client.Documents.Session;
using System;
using System.Web;
using System.Web.Mvc;
namespace Sample.Controllers
{
/// <summary>
/// Controller that saves any changes in the Raven document session if the controller executed successfully.
/// </summary>
public abstract class RavenController : Controller
{
private IAsyncDocumentSession dbSession;
public RavenController()
{
}
/// <summary>
/// Gets the Raven document session for this request.
/// </summary>
public IAsyncDocumentSession DbSession
{
get
{
if (this.dbSession == null)
{
this.dbSession = HttpContext.GetOwinContext().Get<IAsyncDocumentSession>();
}
return dbSession;
}
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (!filterContext.IsChildAction)
{
using (DbSession)
{
if (filterContext.Exception == null && this.DbSession != null && this.DbSession.Advanced.HasChanges)
{
var saveTask = this.DbSession.SaveChangesAsync();
// Tell the task we don't need to invoke on MVC's SynchronizationContext.
// Otherwise we can end up with deadlocks. See http://code.jonwagner.com/2012/09/04/deadlock-asyncawait-is-not-task-wait/
saveTask.ConfigureAwait(continueOnCapturedContext: false);
saveTask.Wait(TimeSpan.FromSeconds(10));
}
}
}
}
}
}