ServerState Session Mode Does Not Handle Session in a Stream Properly

I did some work that adds values ​​to the Session state inside Thread. I would like these values ​​to be available off-stream (obviously).

The information added to the session is available outside the session without any problems when the session state mode is β€œInProc”.

However, when the session state mode is set to "StateServer", the behavior is different. Basically, the values ​​set inside Thread are saved sometimes , and sometimes not . It seems random to me.

Here is the code that reproduces the problem.

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void store_Click(object sender, EventArgs e) { // Set the session values to default. Session["Test1"] = "No"; Session["Test2"] = "No"; // Set the Test1 session value in the thread. ThreadObject threadObject = new ThreadObject() { Username = Page.User.Identity.Name, SessionState = Session }; worker = new Thread(new ParameterizedThreadStart(Work)); worker.Start(threadObject); // Set the Test2 session value in this thread just to compare. Session["Test2"] = "Yes"; } protected void print_Click(object sender, EventArgs e) { // Print out the Session values. label1.Text = string.Empty; label1.Text += "Inside Thread: " + Session["Test1"] + ", \n"; label1.Text += "Outside: " + Session["Test2"] + "\n"; } private static Thread worker; public static void Work(object threadObject) { // Retrieve the Session object and set the Test2 value. ThreadObject threadObject1 = (ThreadObject)threadObject; HttpSessionState currentSession = threadObject1.SessionState; currentSession["Test1"] = "Yes"; } } public class ThreadObject { public string Username { get; set; } public HttpSessionState SessionState { get; set; } } 

The above code works fine with SessionState = "InProc" mode, but is random with:

 <sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" cookieless="false" timeout="20"/> 

Any ideas?

EDIT: According to the comments below, the thread must end before the request is completed (main thread), otherwise everything that is added to the session will be lost. This is because at the end of the main thread, the session is serialized and sent to the data store (Out of Process or SQL Server).

+4
source share
1 answer

I think this is a temporary problem - there is no guarantee that your workflow will be executed by the time you set the values ​​of your labels. To create and run a tread, several MSs are required. By that time, the rest of your main thread has probably completed. You can verify this by setting the debug output and see the execution order of your code.

If you need to ensure that the code in your thread is executing (which eliminates the need for a thread), you can use the WaitOne () thread method so that your main thread waits for the worker thread to return. But depending on the value specified in the thread available in the parallel process, it is risky.

Additionally - IMHO, I would not use threads in ASP.NET applications - I believe that the consensus is that they are a little dangerous. I saw a crash in the application pool due to bad code inside the generated thread.

+1
source

All Articles