执行异步操作asp.net mvc的动作之外 [英] Perform Async operation asp.net mvc outside of the action

查看:103
本文介绍了执行异步操作asp.net mvc的动作之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从对每个请求云数据库加载用户,并可用在使用asp.net mvc的控制器的请求。问题是目前的框架不支持从操作筛选做异步操作。所以OnActionExecuting,OnAuthorization方法不允许我这样做。例如我有以下的code不工作(所以不要尝试)..你得到一个异常:异步模块或处理程序完成而异步操作仍然悬而未决。

I want to be able to load a user from a cloud database on each request and have that available on the request in a controller using asp.net mvc. The problem is the current framework does not support doing async operations from action filters. So OnActionExecuting, OnAuthorization methods do not allow me to do this.. for example I have the following code which DOES NOT work (so don't try it).. You get an exception : "An asynchronous module or handler completed while an asynchronous operation was still pending."

protected async override void OnAuthorization(AuthorizationContext filterContext)
{
  var user = filterContext.HttpContext.User;
  if (!user.Identity.IsAuthenticated)
  {
    HandleUnauthorizedRequest(filterContext);
    return;
  }

  using (var session = MvcApplication.DocumentStore.OpenAsyncSession())
  {
    User currentUser = await session.LoadAsync<User>(user.Identity.Name);
    if (currentUser == null)
    {
      HandleUnauthorizedRequest(filterContext);
      return;
    }

    filterContext.HttpContext.Items["User"] = currentUser;
  }
}

那么,有没有被能够做到这一点任何其他方式?我注意到有一个开始执行方法在基础控制器:

So is there any other way of being able to do this? I notice there is a begin execute method in the base Controller:

protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
{
  return base.BeginExecute(requestContext, callback, state);
}

我能做到这一点有可能?

Could I do it there possibly?

推荐答案

现在的问题是三个月大的,所以我想你已经设法解决这个问题。无论如何,我会在这里我的解决方案,因为我不得不做类似的事情。

The question is three months old so I guess you've managed to work around this. Anyway, I'll add my solution here, as I had to do something similar.

我以前从<一的几种方法href=\"http://$c$c.msdn.microsoft.com/windowsdesktop/Samples-for-Parallel-b4b76364#content\">ParallelExtensionsExtras图书馆。这是我的类:

I used a few methods from the ParallelExtensionsExtras library. This is my class:

public class AsyncControllerBase : Controller
{
    protected override IAsyncResult BeginExecute(System.Web.Routing.RequestContext requestContext, AsyncCallback callback, object state)
    {
        return ExecuteCoreAsync(requestContext, state).ToAsync(callback, state);
    }

    protected override void EndExecute(IAsyncResult asyncResult)
    {
        IAsyncResult baseAsyncResult = ((Task<IAsyncResult>)asyncResult).Result;
        base.EndExecute(baseAsyncResult);
    }

    protected virtual async Task<IAsyncResult> ExecuteCoreAsync(System.Web.Routing.RequestContext requestContext, object state)
    {
        await DoStuffHereOrInDerivedClassAsync();

        var baseBeginExecuteCompletion = new TaskCompletionSource<IAsyncResult>();

        AsyncCallback callback = ar =>
        {
            baseBeginExecuteCompletion.SetResult(ar);
        };

        // OnActionExecuting will be called at this point
        var baseAsyncResult = base.BeginExecute(requestContext, callback, state);

        await baseBeginExecuteCompletion.Task;

        return baseAsyncResult;
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}

这篇关于执行异步操作asp.net mvc的动作之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆