HttpContext.Current and Web Api
Using HttpContext.Current in WebApi is dangerous because of async
HttpContext.Current gets the current context by Thread (I looked into the implementation directly). 提问中的描述
It would be more correct to say that HttpContext is applied to a thread; or a thread "enters" the HttpContext.
Using HttpContext.Current inside of async Task is not possible, because it can run on another Thread. 提问中的描述
Not at all; the default behavior of async/await will resume on an arbitrary thread, but that thread will enter the request context before resuming your async method.
The key to this is the SynchronizationContext. I have an MSDN article on the subject if you're not familiar with it. A SynchronizationContext defines a "context" for a platform, with the common ones being UI contexts (WPF, WinPhone, WinForms, etc), the thread pool context, and the ASP.NET request context.
The ASP.NET request context manages HttpContext.Current as well as a few other things such as culture and security. The UI contexts are all tightly associated with a single thread (the UI thread), but the ASP.NET request context is not tied to a specific thread. It will, however, only allow one thread in the request context at a time.
The other part of the solution is how async and await work. I have an async intro on my blog that describes their behavior. In summary, await by default will capture the current context (which is SynchronizationContext.Current unless it is null), and use that context to resume the async method. So, await is automatically capturing the ASP.NET SynchronizationContext and will resume the async method within that request context (thus preserving culture, security, and HttpContext.Current).
If you await ConfigureAwait(false), then you're explicitly telling await to not capture the context.
Note that ASP.NET did have to change its SynchronizationContext to work cleanly with async/await. You have to ensure that the application is compiled against .NET 4.5 and also explicitly targets 4.5 in its web.config; this is the default for new ASP.NET 4.5 projects but must be explicitly set if you upgraded an existing project from ASP.NET 4.0 or earlier.
You can ensure these settings are correct by executing your application against .NET 4.5 and observing SynchronizationContext.Current. If it is AspNetSynchronizationContext, then you're good; if it's LegacyAspNetSynchronizationContext, then the settings are wrong.
As long as the settings are correct (and you are using the ASP.NET 4.5 AspNetSynchronizationContext), then you can safely use HttpContext.Current after an await without worrying about it.
HttpContext.Current and Web Api的更多相关文章
- System.Web.HttpContext.Current.Session为NULL解决方法
http://www.cnblogs.com/tianguook/archive/2010/09/27/1836988.html 自定义 HTTP 处理程序,从IHttpHandler继承,在写Sys ...
- 为什么获取的System.Web.HttpContext.Current值为null,HttpContext对象为null时如何获取程序(站点)的根目录
ASP.NET提供了静态属性System.Web.HttpContext.Current,因此获取HttpContext对象就非常方便了.也正是因为这个原因,所以我们经常能见到直接访问System.W ...
- HttpContext为null new HttpContextWrapper(System.Web.HttpContext.Current)
HttpContext = (context == null ? new HttpContextWrapper(System.Web.HttpContext.Current) : context);
- System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象
做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 获取路径本来这个方法用的好好的 因为需要 ...
- System.Web.HttpContext.Current.Session获取值出错
在自定义类库CS文件里使用System.Web.HttpContext.Current.Session获取Session时提示错误:未将对象引用设置到对象的实例. 一般情况下通过这种方式获取Sessi ...
- System.Web.HttpContext.Current.Session为NULL值的问题?
自定义 HTTP 处理程序,从IHttpHandler继承,在写System.Web.HttpContext.Current.Session["Value"]的时 候,没有问题,但 ...
- .NET System.Web.HttpContext.Current.Request报索引超出数组界限。
移动端使用Dio发送 FormData, 请求类型 multipart/form-data, FormData内可以一个或多个包含文件时. 请求接口时获取上传的fomdata数据使用 System.W ...
- System.Web.HttpContext.Current.Request用法
public static void SetRegisterSource() { if (System.Web.HttpContext.Current.Request["website&qu ...
- 慎用System.Web.HttpContext.Current
每当控制流离开页面派生的Web表单上的代码的时候,HttpContext类的静态属性Current可能是有用的. 使用这个属性,我们可以获取当前请求(Request),响应(Response),会话( ...
随机推荐
- LeetCode——House Robber
Description: You are a professional robber planning to rob houses along a street. Each house has a c ...
- 图论之最短路径(2)——Bellman-Ford算法
继续最短路径!说说Bellman—Ford算法 思路:假设起点为s,图中有n个顶点和m个边,那么它到任一点(比如i)的最短路径 最多可以有n-1条(没有回路就是n-1条):因为最短路径中不可能包含回路 ...
- java中生成流水号的一个例子(使用BerkeleyDB)
package com.jiaoyiping.berkeleydb; import com.sleepycat.je.*; import com.sleepycat.utilint.StringUti ...
- EUI组件之CheckBox
一.CheckBox常规使用 拖动一个checkBox到exml即可 点击效果 二.代码中监听事件 /** * 主页场景 * @author chenkai 2018/5/26 */ class Ho ...
- 【Android】Android--Dialog
前言 对话框对于应用也是必不可少的一个组件,在Android中也不例外,对话框对于一些提示重要信息,或者一些需要用户额外交互的一些内容很有帮助.本篇博客就讲解一下Android下对话框的使用,在本篇博 ...
- eclipse中切换jdk版本
安装了jdk1.8,但是项目使用的是jdk1.7,需要更改eclipse中的jdk版本 右键项目propeties --- Project facets
- tpcc-mysql安装、使用、结果解读
请点击:http://www.aikaiyuan.com/8687.html 错误处理: ln -s /usr/local/mysql/lib/libmysqlclient.so. /usr/lib6 ...
- js获取当前域名
<script language="javascript"> //获取域名 host = window.location.host; host2=document.do ...
- php 函数__autoload与spl_autoload_register理解
理解自:http://www.cnblogs.com/myluke/archive/2011/06/25/2090119.html __autoload的作用:当我们在一个页面使用其他文件的类方法时候 ...
- 高频访问IP弹验证码架构图 让被误伤的用户能及时自行解封的策略
高频访问IP限制 --Openresty(nginx + lua) [反爬虫之旅] - Silbert Monaphia - CSDN博客 https://blog.csdn.net/qq_29245 ...