1
votes

We are using asp.net session to store data about the progress of the long-term process executed by the user. For some reasons we had to temporarily switch from InProc mode to the SQLServer sessionstate mode. As a result the data being written into the session

public static void UpdateProgressInSession(HttpContext context, string guid, int progress)
    {
        var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;
        if (sessionItem == null)
        {
            context.Session["ProgressStatus"] = new Dictionary<string, int> { { guid, progress } };
            return;
        }
        sessionItem[guid] = progress;
        context.Session["ProgressStatus"] = sessionItem;
    }

is not readable under the session with the same ID but from the another thread.

 public TestRunnerSession GetSessionProgress(HttpContext context, string guid, int testSessionsCount)
    {
        var testRunnerSession = new TestRunnerSession()

        //this variable is always null from now even after session was updated
        var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;
        if (sessionItem != null && testSessionsCount >= MaxSessionsCount && !sessionItem.Keys.Contains(guid))
        {
            testRunnerSession.Status = Currently.Waiting;
            testRunnerSession.ProgressValue = 0;
            return testRunnerSession;
        }
        int value;
        if (sessionItem != null && sessionItem.Count != 0 && sessionItem.TryGetValue(guid, out value))
        {
            testRunnerSession.Status = Currently.InProgress;
            testRunnerSession.ProgressValue = value;
            return testRunnerSession;
        }

        //sessionItem has not been initialized yet
        //This part of the method is always fired now
        testRunnerSession.Status = Currently.InProgress;
        testRunnerSession.ProgressValue = 0;
        return testRunnerSession;
    }

This code works well while in the InProc mode. What could be the source of the problem? Thanks

2

2 Answers

0
votes

I think that the issue isn't in session... Look into this code:

var sessionItem = context.Session["ProgressStatus"] as Dictionary<string, int>;

Probably this initialization wrote into 'sessionItem' null because cann't cast it to Dictionary at once. Try to verify what type you have when getting 'ProgressStatus'