gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:39:47 57 4
gpt4 key购买 nike

我有一个试图检索 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/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com