ASP.NET SessionState SQL Server Issue

321 Views Asked by At

I am using Serialized Model objects of Entity Framework 6 to store in session. The session state is being stored in the database as per these settings:

<sessionState mode="SQLServer"        
              stateConnectionString="tcpip=127.0.0.1:42424"
              sqlConnectionString="data source=MyServer;integrated security=false;user id=MyUser;password=MyPassword;Application Name=MyAppName;"              
              cookieless="false" timeout="20"/>

Some of thees serialized objects does have the composed parents objects too due to Entity Framework relationships. The Composed objects are shown as

"The function evaluation requires all threads to run"

with cross sign during debugging for those composed objects (once clicked on cross, it shows the values). These composed objects (having "The function evaluation requires all threads to run") becomes null on retrieving from session on the next page while the other values are retrieved as expected. Debugging

All this code works with "InProc" Session State as per settings below.

<sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
      </providers>
    </sessionState>

Anyone can help ?

1

There are 1 best solutions below

1
On

You're saving entities with lazy-loaded relations in the session. This won't have worked with InProc (the context used to retrieve the entity will have been disposed when attempting to load the relations), and won't work with SQL: you can't serialize a database connection.

The solutions:

  • Don't store entities in the session, but map them to a model closer to your needs.
  • Do store the entities in the session (you really shouldn't), but explicitly load all relations, so no more database access is needed to read the object's properties.