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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-02-28 Import Example Dataset
2017-02-28 Introduction to MongoDB
2017-02-28 Getting Started with MongoDB (C# Edition)
2017-02-28 本地搭建MongoDB Server