title: Asynchronous task pending in custom action filter?
body: <p>I need your help! I've been a while around this and I haven't been able to figure it out. Let's suppose we have the following controller:</p>
  
  <pre><code>class NotifierController : MyBaseController {
      ... 
      [HttpGet]
      [CanNotifyUser]
      public async Task&lt;ActionResult&gt; NotifyUser(string id)
      {
          return View(await DataBase.Users.FindAsync(id));
      }
      // More actions decorated with the CanNotifyUserAttribute attribute
  }
  </code></pre>
  
  <p><code>DataBase</code> is a property defined in <code>MyBaseController</code> like:</p>
  
  <pre><code>static ApplicationDbContext _appDbContext;
  public ApplicationDbContext DataBase 
  { 
      get { return _appDbContext ?? (_appDbContext = new ApplicationDbContext()); } 
  }
  </code></pre>
  
  <p><code>ApplicationDbContext</code> inherits from <code>IdentityDbContext&lt;MyUser&gt;</code> where <code>MyUser</code> inherits from <code>IdentityUser</code>. So, my problem (I think) comes to the custom action filter <code>CanNotifyUserAttribute</code>, whose <code>OnActionExecuting</code> override is defined as (removing all checks to reduce as much as possible):</p>
  
  <pre><code>public override async void OnActionExecuting(ActionExecutingContext filterContext)
  {
      NotifierController controller = filterContext.Controller as NotifierController;
      Boss boss = await controller.DataBase.Bosses.FindAsync(filterContext.HttpContext.User.Identity.GetUserId());
      object id = filterContext.ActionParameters["id"];
      User user = await controller.DataBase.Users.FindAsync(id.ToString());
      if (user.Department.Id != boss.Department.Id)
      {
          filterContext.Result = new RedirectToRouteResult(
              new RouteValueDictionary(
                  new
                  {
                      action = "Login",
                      controller = "Account",
                      returnUrl = controller.Url.Action("NotifyUser", new { id = id })
                  }));
      }
      base.OnActionExecuting(filterContext);
  }
  </code></pre>
  
  <p>The idea with this attribute is to verify that a boss sends notifications to the users of his department only. Now comes the strange part, this only happens the first time I try to send a notification to an user (no matter whether it's of the same department or not), the error thronw is:</p>
  
  <blockquote>
    <p>[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]</p>
  </blockquote>
  
  <p>If I try (after the error) the same request (i.e., notify the user) it works as expexted either redirecting or sending the notification.</p>
  
  <p>According to <a href="https://stackoverflow.com/questions/28805796/asp-net-controller-an-asynchronous-module-or-handler-completed-while-an-asynchr">this post</a> I might be finishing the request (If I understood well) before an asynchronous task is finished, but all the tasks I've been using are all <code>await</code>ed so I don't know where this error may come from. Hope you can help me. Thanks in advance!</p>