Httpcontext在await函数中变为null [英] Httpcontext is getting null inside await function
本文介绍了Httpcontext在await函数中变为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是使用异步等待的示例。
当我们执行任何代码时,等待httpcontext变为空
我尝试过:
Below is the sample of using async await.
when ever we execute any code inside await httpcontext is getting null
What I have tried:
//Here is the sample of Async Function in which i cant get Httpcontext
public async Task<string> Get(int id)
{
var x = HttpContext.Current;
if (x == null)
{
// not thrown
throw new ArgumentException("HttpContext.Current is null");
}
await Task.Run(() => { GetRealId(); });
return "value";
}
//HttpContext Getting null here..
public void GetRealId()
{
var r = HttpContext.Current;
}
推荐答案
简单的艺术:ASP.NET Web API:HttpContext.Current在任务内部为null [ ^ ](5秒google搜索结果)
The art of simplicity: ASP.NET Web API: HttpContext.Current is null inside Task[^] (result of 5 seconds of googling)
您需要将httpcontext存储在函数范围之外,如类成员或静态。
我想它可能会超时。设置更高的超时。
You need to store the httpcontext outside the scope of your function like a class member or as static.
I guess it may timed out. Set the timeouts higher.
这篇关于Httpcontext在await函数中变为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文