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 AJAX被禁用的原因
- asp.net – 从Application_BeginRequest()中设置后,AsyncLo
- 什么是ASP.NET WebForms相当于ASP.NET MVC的ViewData
- asp.net – 使用CompareValidator控件将用户输入日期与今天
- asp.net – IIS URL重写模块url为小写
- asp.net-mvc-3 – 在MVC和Razor中创建向导步骤
- asp.net – 如何调试Azure 500内部服务器错误[已关闭]
- asp.net-mvc-3 – 带有EF 4.1和EntityState.Modified的MVC3
- ASP.NET MVC – 如何从局部视图中获取当前操作?
- asp.net-mvc – ASP.Net MVC:如何根据原始的Json数据创建一
- asp.net-mvc – 为什么.NET在MVC asp.net应用程序
- asp.net – 如何使用EntityFramework种子数据代码
- dependency-injection – ActionFilterAttribute
- asp.net – 多重约束违反.关系“…”的角色“…”
- ASP.NET MVC – 使用模型中的布尔值禁用Html帮助
- ASP.NET – 可以从服务器代码触发回发吗?
- asp.net-mvc – 如何渲染部分视图到字符串
- asp.net-mvc-3 – ASP .Net MVC 3不引人注目的客
- asp.net-mvc-3 – 在具有默认值的模型上注释属性
- .net – 如何正确处理n层应用程序中的错误?
