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 { /// /// A controller that calls DbSession.SaveChangesAsync() when an action finishes executing successfully. /// 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; } /// /// Executes the action. If no error occurred, any changes made in the RavenDB document session will be saved. /// /// /// /// public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var executedContext = await next.Invoke(); if (executedContext.Exception == null) { await DbSession.SaveChangesAsync(); } } } }