2013-09-23 41 views
0

我正在對使用HttpContext獲取和設置會話變量的方法進行異步調用。但是當我正在使用它時,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); 
     } 

感謝,

Dalvir

回答

1

HttpContext.Current設置的線程上,這意味着你必須把它傳遞到您的異步方法才能使用它因爲處理異步方法的線程不是ASP.NET爲處理請求而創建的線程。