异步和等待在MVC 4控制器 [英] Async and await in MVC 4 Controller

查看:153
本文介绍了异步和等待在MVC 4控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每一次我尝试使用新的异步等待运营商和从数据库返回对象的集合我得到一个无效操作例外。当我用它来只返回一个单一的项目,它工作正常。

控制器code:

 公共异步任务<的ActionResult> EnvironmentList()
{
    EfEnvironmentDataAccess数据访问=新EfEnvironmentDataAccess();
    ICollection的<环境与GT;环境=等待dataAccess.GetAllEnvironmentsAsync();
    返回PartialView(环境);
}
 

查看code:

 < D​​IV CLASS =ECURightCol>
< H3>表转储< / H3>
@ Html.Action(EnvironmentList,环境)
@ Html.Action(COMPUTERLIST,电脑)
@ Html.Action(产品列表,产品)
@ Html.Action(InstanceList,实例)
@ Html.Action(ProfileList文件,档案)
 

数据访问code:

 公开的ICollection<环境与GT; GetAllEnvironments()
{
    使用(EcuWebDataContext DB =新EcuWebDataContext())
    {
        返回db.Environments.OrderBy(E => e.Name).ToList();
    }
}

公共异步任务< ICollection的<环境与GT;> GetAllEnvironmentsAsync()
{
    返回等待Task.Run(()=> GetAllEnvironments());
}
 

我得到的错误是:

  

说明:当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细堆栈跟踪信息,以及它起源于code。

     

异常详细信息:System.InvalidOperationException:HttpServerUtility.Execute阻塞等待异步操作完成

解决方案

首先,不能使用异步处理与儿童的行动的,我想这是你正在做什么。

其次,你没有做任何的异步处理,这里由旋转起来另一个线程与下面的线code执行你的code:

  Task.Run(()=> GetAllEnvironments());
 

这将阻止一个线程在一天结束时,你将什么都没有,但上下文切换的开销。 EF6将有异步处理的支持。对于纯粹的ADO.NET异步查询,看看:

<一个href="http://www.tugberkugurlu.com/archive/asynchronous-database-calls-with-task-based-asynchronous-programming-model-tap-in-asp-net-mvc-4">Asynchronous数据库调用随着基于任务的异步编程模型(TAP)的ASP.NET MVC 4

Every time I try to use the new Async and Await operators and return a collection of objects from a database I get an Invalid Operation exception. When I use it to only return a single Item it works fine.

Controller Code:

public async Task<ActionResult> EnvironmentList()
{
    EfEnvironmentDataAccess dataAccess = new EfEnvironmentDataAccess();
    ICollection<Environment> environments = await dataAccess.GetAllEnvironmentsAsync();
    return PartialView(environments);
}

View Code:

<div class="ECURightCol">
<h3>Table Dumps</h3>
@Html.Action("EnvironmentList", "Environment")
@Html.Action("ComputerList", "Computer")
@Html.Action("ProductList", "Product")
@Html.Action("InstanceList", "Instance")
@Html.Action("ProfileList", "Profile")

The Data Access Code:

public ICollection<Environment> GetAllEnvironments()
{
    using (EcuWebDataContext db = new EcuWebDataContext())
    {
        return db.Environments.OrderBy(e => e.Name).ToList();
    }
}

public async Task<ICollection<Environment>> GetAllEnvironmentsAsync()
{
    return await Task.Run(() => GetAllEnvironments());
}

The Error I get is:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.

解决方案

First of all, you cannot use asynchronous processing with child actions and I suppose this is what you are trying to do.

Secondly, you are not doing any asynchronous processing here by spinning up another thread to execute your code with the below line of code:

Task.Run(() => GetAllEnvironments());

It will block a thread at the end of the day and you will have nothing but a context switch overhead. EF6 will have support for asynchronous processing. For asynchronous queries with pure ADO.NET, have a look:

Asynchronous Database Calls With Task-based Asynchronous Programming Model (TAP) in ASP.NET MVC 4

这篇关于异步和等待在MVC 4控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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