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),会话( ...
随机推荐
- gvim编辑器_vimrc文件
set nocompatiblesource $VIMRUNTIME/vimrc_example.vimsource $VIMRUNTIME/mswin.vimbehave mswin set dif ...
- [算法] N 皇后
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- mysql如何按周统计数据?
转自:https://www.cnblogs.com/wanghetao/p/3920124.html MySql 按周/月/日统计数据的方法 知识关键词:DATE_FORMAT select DA ...
- 增量式PID的matlab实现
首先,增量式PID的实现公式: 式中 Δe(k)=e(k)-e(k-1) 进一步可以改写成 式中 . . 为了便于理解,也可写成: 式中e(k)为第k次采样时的设定值与实际值的差,e(k-1 ...
- php 常用的系统函数
字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...
- Requested bean is currently in creation: Is there an unresolvable circular reference?
spring容器初始化报错:循环依赖,错误信息如下: Requested bean is currently in creation: Is there an unresolvable circula ...
- 170508、忘记jenkins密码或者修改jenkins密码
刚配置好jenkins时,不知道密码是什么,很多同学都有这种烦恼把,各种抓狂. 操作步骤: 1.进入jenkins用户目录 cd /home/rick/.jenkins/users/admin ps ...
- Code Forces 149DColoring Brackets(区间DP)
Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- python的pip的配置文件路径在哪里?如何修改pypi源?
官方文档: https://pip.pypa.io/en/stable/user_guide/#configuration 举个例子: Windows用户想要更改pypi源,可以在%APPDATA%目 ...
- 【selenium】selenium ide的安装过程
简介一:SeleniumIDE安装 1.安装seleniumIDE,打开火狐浏览器,地址栏输入地址—>点击Add按钮—>安装结束后重启FireFox—>在菜单栏中可以看到Seleni ...