How do I access HttpContext.Current in Task.Factory.StartNew?

后端 未结 3 1733
鱼传尺愫
鱼传尺愫 2020-12-08 15:23

I want to access HttpContext.Current in my asp.net application within

Task.Factory.Start(() =>{
    //HttpContext.Current is null here
});
3条回答
  •  爱一瞬间的悲伤
    2020-12-08 16:21

    As David pointed out, HttpContext.Current will not work all the time. In my case, about 1 of 20 time, CurrentContext will be null. End up with below.

    string UserName = Context.User.Identity.Name;
    
    System.Threading.Tasks.Task.Factory.StartNew(() =>
    {
        UserName ...
    }
    

提交回复
热议问题