Async PartialView causes quot;HttpServerUtility.Execute blocked...quot; exception(Async PartialView 导致“HttpServerUtility.Execute 受阻...例外)
问题描述
我有一个局部视图,它尝试使用异步从数据库中检索 IEnumerable<Post>
...
方法
公共静态类 PostService{公共静态 int PostsPerPage = 50;公共静态异步任务<IEnumerable<Post>>GetRecentAsync(int page = 0){返回等待 entityFrameworkDbContext.Posts.ToListAsync();}}
局部视图
公共异步任务<ActionResult>最近(int page = 0){返回 PartialView(等待 PostService.GetRecentAsync(page));}
然后如果我尝试调用它
@Html.Action("Recent", "Post")
我得到以下异常
<块引用>HttpServerUtility.Execute 在等待异步操作完成时被阻塞.
说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.InvalidOperationException:HttpServerUtility.Execute 在等待异步操作完成时被阻塞.
为什么会出现此错误?它不应该工作吗?
子动作必须同步调用.Issue 601 我也不知道最近对当前 MVC 库的任何更新允许此功能.
对问题 601 的评论,暗示此功能将添加到 MVC vNext,又名.MVC6.子动作看起来被替换为 ViewComponent
可以从如下视图异步调用.完整示例这里和这里
@await Component.InvokeAsync("YourComponent")</div>有关 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 受阻..."例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Async PartialView 导致“HttpServerUtility.Execute 受阻..."例外


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01