- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
更新
经过一番思考,我得出的结论是,我可能没有提出正确的问题。
给出以下代码:
public class HomeController : Controller
{
public Task<string> DownloadAsync(string url)
{
using (var web = new WebClient())
{
return web.DownloadStringTaskAsync(url);
}
}
// THROWS EXCEPTION
public ActionResult Index()
{
var data = DownloadAsync("http://google.dk");
return Content(data.Result);
}
// WORKS
public async Task<ActionResult> IndexWorks()
{
var data = await DownloadAsync("http://google.dk");
return Content(data);
}
}
很明显(尤其是在阅读@Stephen Cleary 的博客文章之后),ActionResult Index()
代码会导致死锁。但为什么呢?
经过一番挖掘,我发现 .NET 4.5
推出新的AspNetSynchronizationContext
,这应该更加“任务友好”。下载.NET 4.5
的源代码看看新的AspNetSynchronizationContext
内部,我认为调用 OperationStarted
,将导致检查名为 AllowVoidAsyncOperations
的 bool 值。如果该值为 true,则没有问题。然而,如果这个 bool 值的值为 false,它将抛出以下异常:
An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async=\"true\" %>.
经过深思熟虑,我想通了,调用 async Task<ActionResult> IndexWorks()
以某种方式设置 AllowVoidAsyncOperations
为真。 - 调用同步版本时,它保持默认值:false。
因此我的问题是:
异步ActionResult什么时候调用AspNetSynchrnoizationContext
的内部方法,即设置 AllowVoidAsyncOperations
为真? - 到目前为止,我已将范围缩小到类 CallHandlerExecutionStep
里面HttpApplication
类(class)。 - 但是,我不确定它如何决定是否允许。
最佳答案
如果您尝试在请求生命周期中不允许的点(或完全在请求上下文之外)执行异步操作,则 ASP.NET 会引发此特定异常。
您不应该在 ASP.NET MVC 中看到这一点。有两件事需要检查:
UseTaskFriendlySynchronizationContext
set to true
or have httpRuntime.targetFramework
set to 4.5
.更新:经过反射(reflection),在 ASP.NET MVC 中还有另外两种情况可能导致此问题:
关于c# - 调用异步 ActionResults 时,AllowVoidAsyncOperations 何时设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17013536/
我有一个 ActionResult 调用另一个 ActionResult。 我在我的 case 语句中调用了一个 ActionResult,但它不起作用。这是我所拥有的: public Acti
我有一个 ActionResult 扩展,它在返回页面时向 TempData 添加 toast: public static IActionResult WithMessage(this Action
假设我有以下代码,在记事本中模拟,所以请原谅任何小错误:) //Default page public ActionResult Index() { var musicView
我有一个来自数据库的对象,它在我的应用程序的很多地方使用。 实际的精确对象构建起来有点复杂,尤其是在开发过程中,我已经多次更改它。为此,我将方法从 Controller 中提取出来,并构建了一个具有对
我像这样向 GET ActionResult 发送参数: public ActionResult MyFormLetter(string studentName, string teacherName
在 Scott Hanselman 的书中(第 1 章),他为我们提供了两个选项来实现 Create 操作方法的 [HttpPost]。 第一个依赖于 TryUpdateModel 根据传入的表单字段
错误在下面代码的第 5 行: public class FirstController : Controller{ public async Task Index() {
我得到了简单的网络服务应用程序: public class MyController : Controller { public ActionResult balance( int amount)
所以我有这个 Javascript 代码: function AddToStuff(somethingID) { var stuffID = document.getElementById('
我试图在选中复选框时调用 ActionResult,但我不知道为什么它不起作用下面是我的代码。 --复选框 --模态
我正在使用 MVC 4 创建游戏。有一个犯罪功能,但我无法解决。我已经尝试了很多东西,但由于我是 MVC4 的新手,所以我无法弄明白。 我创建了一个单选按钮列表,每个不同的选项应该给出不同的结果。这是
我知道有很多类似的问题,但我没有在其中任何一个中得到我想要的答案。我想按原样将 ActionResult 转换为 String,方法如下: public ActionResult ProductDet
我想从一些操作方法中返回一个强制当前页面刷新的结果。 我写这篇文章是为了获得这样的结果: public class RefreshResult : ActionResult { pu
我使用 MVC 5 返回 Json。到达数据返回点的总时间为 40 毫秒。 然而,即使在服务器上运行它,浏览器也需要 6000 毫秒 才能获取数据。 我的问题是返回值后会发生什么。我正在尝试找出导致速
我创建了一个派生自 ActionResult 的 SitemapResult 类。它允许调用者添加任意数量的 URL 资源,然后以 XML 格式输出站点地图数据。 public class Sitem
例如,对于在线 Web 应用程序,我只需要服务器驱动器 X:\Docs 上文件夹中的文档。有没有一种方法可以让网站上的按钮默认打开 X:\Docs?我试过打开特定文件夹但没有成功: [HttpPost
我的 Controller 中有以下内容; public ActionResult CocktailLoungeBarAttendant() { return View
我是 MVC 的新手,正在尝试从 Controller 调用存储过程。 在模型中,我有 edmx,它将所有存储过程作为函数 Cannot implicitly convert type System.
这个有效: public ActionResult Edit(int id, CompPhone cmpPhn) { var vM = new MyViewModel(); if (cmpPh
假设我有应用程序 .net Core 2.1 Web API + Angular 7 为什么我应该总是返回 ActionResult? 这之间有什么区别吗: public ActionResult G
我是一名优秀的程序员,十分优秀!