会话访问和异步回调 [英] Session Access and Async Callbacks

查看:104
本文介绍了会话访问和异步回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,因此我在网站的目标网页上进行了许多异步Web服务调用.我想将这些调用的结果设置为session,以便以后可以使用它们,但是我不能,因为在回调中HttpContext.Current为null.基本上,我想做这样的事情:

Ok, so I have a bunch of async web service calls that I make on the landing page of my website . 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. 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();
}

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

异步调用GetLoansCallback()GetLoansAsync,如下所示:

Which asynchronously invokes GetLoansAsync, whose callback is GetLoansCallback(), shown here:

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.

http://blogs.msdn.com/b/tmarq/archive/2010/04/14/performing-asynchronous-work-or-tasks-in-asp-net-applications.aspx

基本上,它说HttpContext在工作完成后(即在回调中)不可用,并且看起来您必须将会话操作移到GetLoansAsync方法中.

Basically, it says that HttpContext cannot be available upon the work completion (i.e. in the callback) and it looks like that you will have to move the session operations into the GetLoansAsync method.

这篇关于会话访问和异步回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆