1

好的,所以我在网站的登录页面上进行了一堆异步 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
}

我无法实现自定义会话提供程序,所以我必须坚持我所拥有的。就目前而言,我无法在异步回调中为会话设置任何内容。有没有办法解决这个问题?

4

1 回答 1

2

你可以看看这篇博文

基本上,它表示HttpContext在工作完成时(即在回调中)不可用,看起来您必须将会话操作移到GetLoansAsync方法中。

于 2012-08-10T18:24:53.023 回答