Async PartialView 导致“HttpServerUtility.Execute 被阻塞...";例外 [英] Async PartialView causes "HttpServerUtility.Execute blocked..." exception

查看:23
本文介绍了Async PartialView 导致“HttpServerUtility.Execute 被阻塞...";例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个局部视图尝试使用异步从数据库中检索 IEnumerable...

方法

公共静态类 PostService{公共静态 int PostsPerPage = 50;public static async Task>GetRecentAsync(int page = 0){返回等待 entityFrameworkDbContext.Posts.ToListAsync();}}

局部视图

public async Task最近(整数页 = 0){返回 PartialView(await PostService.GetRecentAsync(page));}

然后如果我尝试调用它

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

我收到以下异常

<块引用>

HttpServerUtility.Execute 在等待异步操作完成时被阻塞.

描述:在执行当前 Web 请求的过程中发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息.

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

为什么会出现这个错误?不应该吗?

解决方案

子操作必须同步调用.问题 601 我也不知道当前 MVC 库有任何允许此功能的最新更新.

对问题 601 的评论暗示此功能被添加到 MVC vNext,又名.MVC6.子操作看起来被替换为 ViewComponent ,它可以从如下视图中异步调用.完整示例此处这里

@await Component.InvokeAsync("YourComponent")

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

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

I have a partial view that tries to retrieve a IEnumerable<Post> from the database using async...

Method

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

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

PartialView

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

And then if I try to call it

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

I get the following exception

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.

Why do I get this error? Shouldn't it work?

解决方案

Child actions must be invoked synchronously. Issue 601 I am also not aware of any recent updates to the current MVC libraries allowing this functionality.

A comment on Issue 601, hints at this functionality being added in MVC vNext, aka. MVC6. Child actions look to be replaced with ViewComponent which can be invoked asynchronously from a view as below. Full examples here and here

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

For more on MVC6 check out, http://www.asp.net/vnext/overview/aspnet-vnext/overview

Note: This answer is just a formality, so the question can be marked as answered

这篇关于Async PartialView 导致“HttpServerUtility.Execute 被阻塞...";例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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