简体   繁体   English

会话访问和异步回调

[英]Session Access and Async Callbacks

Ok, so I have a bunch of async web service calls that I make on the landing page of my website.好的,所以我在网站的登录页面上进行了一堆异步 Web 服务调用。 I want to set the results of these calls to session so that I can use them later, but I can't because HttpContext.Current is null in the callback.我想将这些调用的结果设置为会话,以便我以后可以使用它们,但我不能,因为回调中的HttpContext.Current为 null。 Basically, I want to do something like this:基本上,我想做这样的事情:

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();
}

Where GetLoans() looks like this: 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;
}

Which asynchronously invokes GetLoansAsync , whose callback is GetLoansCallback() , shown here:其中异步调用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
}

I can't implement a custom session provider, so I have to stick with what I've got there.我无法实现自定义会话提供程序,因此我必须坚持使用现有的方法。 So as it stands, I can't set anything to session in my async callback.因此,就目前而言,我无法在我的异步回调中为会话设置任何内容。 Is there a way to get around this?有办法解决这个问题吗?

You might take a look at this blog post .您可以看看这篇博文

Basically, it says that HttpContext cannot be available upon the work completion (ie in the callback) and it looks like that you will have to move the session operations into the GetLoansAsync method.基本上,它表示HttpContext在工作完成时不可用(即在回调中),看起来您必须将会话操作移到GetLoansAsync方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM