c# - Async PartialView 导致 "HttpServerUtility.Execute blocked..."异常

标签 c# asp.net-mvc entity-framework asp.net-mvc-5 entity-framework-6

我有一个试图检索 IEnumerable<Post> 的部分 View 使用异步从数据库...

方法

public static class PostService
{
    public static int PostsPerPage = 50;

    public static async Task<IEnumerable<Post>> GetRecentAsync(int page = 0)
    {
        return await entityFrameworkDbContext.Posts
            .ToListAsync();
    }
}

局部 View

public async Task<ActionResult> Recent(int page = 0)
{
    return PartialView(await PostService.GetRecentAsync(page));
}

然后如果我尝试调用它

@Html.Action("Recent", "Post")

我得到以下异常

HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.

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.

为什么会出现此错误?它不应该起作用吗?

最佳答案

子 Action 必须同步调用。 Issue 601我也不知道当前 MVC 库的任何最新更新允许此功能。

Issue 601 的评论暗示此功能已添加到 MVC vNext 中,又名。 MVC6。子操作看起来被替换为 ViewComponent ,它可以从如下 View 异步调用。完整示例 herehere

<div>
@await Component.InvokeAsync("YourComponent")
</div>

有关 MVC6 的更多信息,请查看 http://www.asp.net/vnext/overview/aspnet-vnext/overview

注意:这个回答只是走个形式,所以问题可以标记为已回答

关于c# - Async PartialView 导致 "HttpServerUtility.Execute blocked..."异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072720/

相关文章:

javascript - 强制浏览器在 asp.net 应用程序中获取最新的 js 和 css 文件

c# - 无法添加到 IPgedList 对象或将 List<T> 传输到 IPgedList<T>

linq - 如何使用linq按多列分组

c# - SaveChanges() 正在保存 Unchanged 记录

entity-framework - 如何为多对多关系表指定模式?

c# - 是否可以在没有代码覆盖工具作为入口点的情况下收集代码覆盖数据?

c# - 在属性名称中使用点反序列化 JSON

asp.net-mvc - 如何在 MVC 应用程序中切换身份验证?

c# - 如何以不同方式呈现类的派生类型?

c# - 使用星期几获取日期