-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathRavenController.cs
43 lines (39 loc) · 1.63 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
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Raven.Client.Documents.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sample.Mvc.Controllers
{
/// <summary>
/// A controller that calls DbSession.SaveChangesAsync() when an action finishes executing successfully.
/// </summary>
public class RavenController : Controller
{
public RavenController(IAsyncDocumentSession dbSession)
{
this.DbSession = dbSession;
// RavenDB best practice: during save, wait for the indexes to update.
// This way, Post-Redirect-Get scenarios won't be affected by stale indexes.
// For more info, see https://ravendb.net/docs/article-page/4.2/csharp/client-api/session/saving-changes
this.DbSession.Advanced.WaitForIndexesAfterSaveChanges(timeout: TimeSpan.FromSeconds(5), throwOnTimeout: false);
}
public IAsyncDocumentSession DbSession { get; private set; }
/// <summary>
/// Executes the action. If no error occurred, any changes made in the RavenDB document session will be saved.
/// </summary>
/// <param name="context"></param>
/// <param name="next"></param>
/// <returns></returns>
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var executedContext = await next.Invoke();
if (executedContext.Exception == null)
{
await DbSession.SaveChangesAsync();
}
}
}
}