HttpContext.Current为空当的方法从PageAsyncTask称为 [英] HttpContext.Current is null when in method called from PageAsyncTask

查看:218
本文介绍了HttpContext.Current为空当的方法从PageAsyncTask称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我有一个按钮的点击,在按钮打开的对话​​框的形式打开一个对话框,页面点击我可以从选定的.txt文件中读取数据的清单,并建立一个查询并添加数据到一些数据库表。由于可能有大量的数据,这个​​过程可能需要因为这个用户的大的时间就不能,直到上载完成的应用程序的工作。因此Asynk我使用PageAsyncTask使上传过程。下面是code样品,但在堪称PageAsyncTask的方法HttpContext.Current为null,所以我不能够使用会话处理。请有这方面的指导,为什么会变成这样空,我怎么能使用会话在这种情况下

 保护无效BtnUpload_click(对象发件人,EventArgs的发送)
    {
        PageAsyncTask asyncTask1 =新PageAsyncTask(OnBegin,OnEnd,OnTimeout,SessionManager.UserData,真正的);        Page.RegisterAsyncTask(asyncTask1);
        Page.ExecuteRegisteredAsyncTasks();
    }公共IAsyncResult的OnBegin(对象发件人,EventArgs的,
            CB的AsyncCallback,对象extraData)
    {
        _taskprogress =AsyncTask的开始时间:+ DateTime.Now +;。
        UDATA = extraData为的UserData;        _dlgt =新AsyncTaskDelegate(BeginInvokeUpload);
        IAsyncResult的结果= _dlgt.BeginInvoke(CB,extraData);        返回结果;
    }私人无效BeginInvokeUpload()
    {
        字符串selectedFileName =的String.Empty;
        字符串returnValuePage =的String.Empty;
        用户取款=新用户();
        SessionManager.UserData = UDATA;
    }  私人无效BeginInvokeUpload()
    {
        字符串selectedFileName =的String.Empty;
        字符串returnValuePage =的String.Empty;
        用户取款=新用户();
        SessionManager.UserData = UDATA;
    }公共类是SessionManager
    {
公共静态的UserData的UserData
        {
            得到
            {
                用户数据的UserData = NULL;
                如果(HttpContext.Current.Session [的UserData]!= NULL)
                {
                    用户数据= HttpContext.Current.Session [的UserData]作为的UserData;
                }
                返回用户数据;
            }
            组
            {
                 HttpContext.Current.Session [的UserData] =值;
            }
        }
}


解决方案

答案很简单:你不能使用会话如果 HttpContext.Current为null

所以,如果你需要修改会话你简单的不能和唯一的选择就是让你完全自定义会话模块/解决方案。

如果你只需要阅读一些值,那么你可以在创建你的线程将它们传递。

最后唯一的解决办法是,如果你赢了操纵会话变量不使用线程。

为什么要这样设计?

为什么MS会议并没有让您处理它的网页和一个线程内的一面呢?答案是,因为是需要锁定在页面处理的会话数据 - 与此锁,即使你启动一个线程,并能够获得会话数据,将不能够使用它平行

此外,如果你能使用会话你自己在一个线程,那么这个线程可能锁定整个页面视图的过程,因为我再说一遍,会话锁定整个页面视图,并且每个使用相同页面会话没有在并行工作

这整个页面上的会话锁是必要的方式MS会话工作,并避免那就是让一个完全自定义会话的解决方案,并处理特殊情况用不同的code的唯一途径。

有关设计好是你避免做出了很多由你自己在每一页上的呼叫锁定和同步的 - 例如,如果你禁用页面上的会话,并使用该页面进行数据接入,如果使用使上插入多个双击,你不同步处理它的插入,你结束了多个相同的插入。

更多关于会话锁:结果
<一href=\"http://stackoverflow.com/questions/8989648/replacing-asp-nets-session-entirely/9021543#9021543\">Replacing ASP.Net会话完全结果
<一href=\"http://stackoverflow.com/questions/9426673/web-app-blocked-while-processing-another-web-app-on-sharing-same-session/9427550#9427550\">Web应用程序被阻止在处理上共享同一个会话结果另一个Web应用程序
<一href=\"http://stackoverflow.com/questions/9052401/jquery-ajax-calls-to-web-service-seem-to-be-synchronous/9052457#9052457\">jQuery Ajax调用到Web服务似乎是同步结果
<一href=\"http://stackoverflow.com/questions/12284530/asp-net-server-does-not-process-pages-asynchronously/12285168#12285168\">ASP.NET服务器不处理页面异步

类似的问题:结果
<一href=\"http://stackoverflow.com/questions/2771536/how-to-get-session-data-with-out-having-httpcontext-current-by-sessionid\">How具有了HttpContext.Current,通过的SessionID 得到会话数据

I have a scenario where i have a page which opens a dialog on click of a button, in the opened dialog form on button click i can read a list of data from a selected .txt file and build a query and add the data to some database tables. Since there could be large amount of data this process can take large time because of this the user would not be able to work on the application until the upload completes. Hence to make the upload process Asynk i am using the PageAsyncTask. Below is the code sample, but in the method called in the PageAsyncTask the HttpContext.Current is null hence i am not able to use session handling. Please any guidance on this why would this be null and how can i use the session in this case

   protected void BtnUpload_click(object sender, EventArgs e)
    {
        PageAsyncTask asyncTask1 = new PageAsyncTask(OnBegin, OnEnd, OnTimeout, SessionManager.UserData, true);

        Page.RegisterAsyncTask(asyncTask1);
        Page.ExecuteRegisteredAsyncTasks();
    }

public IAsyncResult OnBegin(object sender, EventArgs e,
            AsyncCallback cb, object extraData)
    {
        _taskprogress = "AsyncTask started at: " + DateTime.Now + ". ";
        uData = extraData as UserData;

        _dlgt = new AsyncTaskDelegate(BeginInvokeUpload);
        IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);

        return result;
    }

private void BeginInvokeUpload()
    {
        string selectedFileName = string.Empty;
        string returnValuePage = string.Empty;
        User teller = new User();
        SessionManager.UserData = uData;
    }



  private void BeginInvokeUpload()
    {
        string selectedFileName = string.Empty;
        string returnValuePage = string.Empty;
        User teller = new User();
        SessionManager.UserData = uData;
    }

public class SessionManager
    {
public static UserData UserData
        {
            get 
            {
                UserData userData = null;
                if (HttpContext.Current.Session["UserData"] != null)
                {
                    userData = HttpContext.Current.Session["UserData"] as UserData;                    
                }
                return userData;
            }
            set 
            {
                 HttpContext.Current.Session["UserData"]=value;                  
            }
        }
}

解决方案

The answer is simple : you can not use the session if the HttpContext.Current is null

So if you need to modify the session you simple can not and the only alternative is to make your totally custom session module/solution.

If you only need to read some values, then you can pass them when you create your thread.

And finally the only solution is to not use the thread if you won to manipulate the session variables.

why this design?

why MS session did not allow you to handle it out side of a page and inside a thread ? the answer is because is need to lock the session data on page processing - with this lock even if you start a thread and been able to get the session data, will not been able to use it parallel.

Also if you been able to use the session your self in a thread, then this thread may lock the entire page view process, because I say it again, session is lock the entire page view, and each page that use the same session are not work in parallel

This lock of session on the entire page is necessary the way the MS session works, and the only way to avoid that is to make a totally custom session solution, and handle special cases with different code.

The good about that design is that you avoid to make a lot of locking and synchronization by your self on every page call - for example if you disable the session on a page, and use that page for data inserting, if a use make multiple double clicks on the insert, and you do not handle it with synchronization on the insert, you end up with multiple same insertions.

More about session lock:
Replacing ASP.Net's session entirely
Web app blocked while processing another web app on sharing same session
jQuery Ajax calls to web service seem to be synchronous
ASP.NET Server does not process pages asynchronously

Similar question:
How to get Session Data with out having HttpContext.Current, by SessionID

这篇关于HttpContext.Current为空当的方法从PageAsyncTask称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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