[英]Session Access and Async Callbacks
好的,所以我在網站的登錄頁面上進行了一堆異步 Web 服務調用。 我想將這些調用的結果設置為會話,以便我以后可以使用它們,但我不能,因為回調中的HttpContext.Current
為 null。 基本上,我想做這樣的事情:
public ActionResult Something()
{
GetLoans(); //each of these is async
GetPartInfo(); //and sets its result to session
GetOtherAsyncStuff(); //(or at least it needs to)
//lots of other stuff
Return View();
}
GetLoans()
看起來像這樣:
public IAsyncResult GetLoans()
{
IAsyncResult _ar;
GetLoansDelegate d_Loans = new GetLoansDelegate(GetLoansAsync);
_ar = d_Loans.BeginInvoke(parameter1,parameter2, GetLoansCallback, new object()); //new object() is just a placeholder for the real parameters im putting there
return _ar;
}
其中異步調用GetLoansAsync
,其回調為GetLoansCallback()
,如下所示:
private void GetLoansCallback(IAsyncResult ar)
{
AsyncResult result = (AsyncResult)ar;
GetLoansDelegate caller = (GetLoansDelegate)result.AsyncDelegate;
List<Loan> loans = caller.EndInvoke(ar);
Session["Loans"] = loans; //this call blows up, since HttpContext.Current is null
}
我無法實現自定義會話提供程序,因此我必須堅持使用現有的方法。 因此,就目前而言,我無法在我的異步回調中為會話設置任何內容。 有辦法解決這個問題嗎?
您可以看看這篇博文。
基本上,它表示HttpContext
在工作完成時不可用(即在回調中),看起來您必須將會話操作移到GetLoansAsync
方法中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.