如何使用 HttpContext 进行异步调用

本文关键字:异步 调用 何使用 HttpContext | 更新日期: 2023-09-27 18:32:20

我正在对一个方法进行异步调用,我想在其中使用 HttpContext 来获取和设置会话变量。但是当我对它进行签名时,HttpContext显示空。那么我如何使用HttpContext呢?

法典:

public void SessionsFetchAllWatchNowAsync()
        {
            string aaa = Convert.ToString(HttpContext.Current.Session["UserID"]);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Session[Utility.UserID] = 0;
            Action<StartPage> notifyTask = null;
            notifyTask = new Action<StartPage>((o) => new StartPage().SessionsFetchAllWatchNowAsync());
            if (notifyTask != null)
                notifyTask.BeginInvoke(null, null, null);
        }

谢谢

达尔维尔

如何使用 HttpContext 进行异步调用

HttpContext.Current设置在

线程上,这意味着您必须将其传递给异步方法才能使用它,因为处理异步方法的线程不是 ASP.NET 创建的线程来处理您的请求。