问题描述
我有一个局部视图,它尝试使用异步从数据库中检索 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 受阻..."例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!