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:
class NotifierController : MyBaseController {
...
[HttpGet]
[CanNotifyUser]
public async Task<ActionResult> NotifyUser(string id)
{
return View(await DataBase.Users.FindAsync(id));
}
// More actions decorated with the CanNotifyUserAttribute attribute
}
DataBase
is a property defined in MyBaseController
like:
static ApplicationDbContext _appDbContext;
public ApplicationDbContext DataBase
{
get { return _appDbContext ?? (_appDbContext = new ApplicationDbContext()); }
}
ApplicationDbContext
inherits from IdentityDbContext<MyUser>
where MyUser
inherits from IdentityUser
. So, my problem (I think) comes to the custom action filter CanNotifyUserAttribute
, whose OnActionExecuting
override is defined as (removing all checks to reduce as much as possible):
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);
}
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:
[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]
If I try (after the error) the same request (i.e., notify the user) it works as expexted either redirecting or sending the notification.
According to this post 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 await
ed so I don't know where this error may come from. Hope you can help me. Thanks in advance!