Httpcontext在await函数中变为null

编程入门 行业动态 更新时间:2024-10-27 13:31:45
本文介绍了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

本文发布于:2023-11-16 09:45:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603159.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Httpcontext   await   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!