Updating Asp.net session variables by stream is not reflected in the session

In my page1.aspx, I create a report from the database using a stream.

//on button click Hashtable ht = (Hashtable)Session["ReportParam"]; ReportThreadClass rth = new ReportThreadClass(ht); Thread thread = new System.Threading.ThreadStart(rth .Run); thread.Start(); 

In my rum class class, I am updating the values ​​in the Hashtable, how many pages I have created.

 //in thread' method public virtual void Run() { int pagecount=0; while(done) { //loading data from DB and generating html pages ht["Total_Pages"] = pagecount; } } 

On my Page2.aspx I read the values ​​from the session variable

 Hashtable ht = (Hashtable)Session["ReportParam"]; int TotalPages = (int) ht["Total_Pages"]; 

When I run the code in InProc mode, every thing works fine. I get updated values ​​from the session. Since each thing is stored in a static variable, and ht refers to Session, so it is automatically updated in the session (HashTable does not need to be reassigned to the session).

But when I run the code on the state server (OutProc mode), it needs to store the session data in another process, serializing the Hash table.

But the value of Total_Pages is not updated in Page2.aspx even after Thread is fully executed.

So, is there any event or method that runs to store all updates in the session variable to the server-state, if so, then tell me. if not, pls will offer me some idea to get the updated value in page2.aspx.

+4
source share
3 answers

In Out Proc mode, the session is saved after some event, so if the stream updates your session variables, it will not be stored in memory.

If u uses Inproc Mode, then save the session in a static dictionary, so if your thread updates it, u will get the updated value on any page.

So, you have two solutions for this situation.

  • Use inProc mode
  • Maintaining a dictionary in your thread class with the key as the session identifier, and the value is your hash table. Therefore, if page2.aspx wants to read the value of the hash table, then it will pass its session identifier to the method and which will return the required value.
0
source

I would choose SET and GET SessionState as follows:

In your run thread

 // no complex object like hastable, just a plain value... Session["pageCount"] = pageCount; 

In your page2.apsx:

 var pageCount = (int) Session["pageCount"]??0; 

The reason the report stream does not update its session value when using an out-of-second session state is because the session has no way to detect the hash table with the changed value, so it does not update the underlying storage using the serialized version of hastable. When you discover one immutable object, it will persist if the value is changed;

Since the session may already disappear when your thread finishes configuring begtter, you need to get a link to SqlSessionStateStore and call SetAndReleaseItemExclusive. Ultimately, you may need an overloaded SessionStateProvider that can handle your script.

+1
source

Less efficient, but I'll probably just run a database for page counting on page 2.

Or create a separate session value to count the pages on page 1 while doing the rest. (EDIT: Nevermind is the second part that Rene suggested below).

0
source

Source: https://habr.com/ru/post/1481192/


All Articles