asp.net-mvc – 异步HttpModule MVC
我有一个包含以下代码的同步HttpModule.
/// <summary> /// Occurs as the first event in the HTTP pipeline chain of execution /// when ASP.NET responds to a request. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">An <see cref="T:System.EventArgs">EventArgs</see> that /// contains the event data.</param> private async void ContextBeginRequest(object sender,EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; await this.ProcessImageAsync(context); } 当我尝试从空的MVC4应用程序(NET 4.5)运行该模块时,我收到以下错误.
我似乎错过了一些东西但是通过我的阅读,错误实际上不应该发生. 我有一个挖掘,但我似乎无法找到任何帮助,有没有人有任何想法? 解决方法
因此,您在同步HttpModule事件处理程序中具有异步代码,并且ASP.NET抛出异常,指示异步操作只能在异步处理程序/模块中启动.对我来说似乎很简单.
要解决此问题,您不应直接订阅BeginRequest;相反,创建一个返回任务的“处理程序”,将其包装在 像这样的东西: private async Task ContextBeginRequest(object sender,EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; await ProcessImageAsync(context); // Side note; if all you're doing is awaiting a single task at the end of an async method,// then you can just remove the "async" and replace "await" with "return". } 并订阅: var wrapper = new EventHandlerTaskAsyncHelper(ContextBeginRequest); application.AddOnBeginRequestAsync(wrapper.BeginEventHandler,wrapper.EndEventHandler); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – 带有EF 4.1和EntityState.Modified的MVC3
- asp.net-mvc – Unity PerRequestLifetimeManager在不同请求
- ASP.NET会话状态在异常后丢失
- asp.net-mvc – 捕获路由错误
- IIS虚拟目录和ASP.NET目录路径
- 如何在不使用MembershipProvider的情况下使用ASP.NET登录控
- asp.net-mvc-2 – RedirectToRouteResult如何工作?
- asp.net – 无法打开登录请求的数据库.登录失败.用户登录失
- asp.net – 何时使用HttpApplicationState而不是Web.Cachin
- ASP.NET AJAX进度条:从代码背后更新?
- asp.net-mvc-3 – 具有多个强类型部分视图的MVC
- asp.net-mvc – 获取视图名称,其中ViewResult.Vi
- asp.net – 在SQL SERVER 2005上启用CLR是否存在
- 每次在ASP.NET中触发哪个事件
- asp.net-mvc – @ Html.DropDownListFor如何添加
- asp.net-mvc – 为什么使用sendgrid发送的电子邮
- asp.net-mvc – 链接到iPad上的Google Document
- asp.net-web-api – WebApi 2超出最大请求长度
- asp.net-mvc – 扩展实体框架应用程序/多个应用程
- asp.net-mvc – 通过删除空格打破我的calc语句的