title: Highly unusual ASP.net Session variable persistence
body: <p>I've got a really really strange ASP.net intermittent session persistence problem. First some facts:</p>
  
  <p>Environment: </p>
  
  <ul>
  <li>F5 Load Balancer with sticky sessions enabled: </li>
  <li>Web Servers: 2 *
  Windows Server 2008 R2, </li>
  <li>.net Framework Version: 2.0.50727, IIS7, </li>
  <li>Database Server: SQL Server 2008 R2 </li>
  <li>Session Protocol: ASP.net InProc sessions, timeout set to 20mins, cookieless=false </li>
  <li>CMS: Ektron CMS
  400.net</li>
  </ul>
  
  <p>Ok on with the problem.</p>
  
  <p>We have some custom VB.net session values which get set in our website for a shopping cart. from time to time SOME of these variables seem to lose persistence. I say some because the main session remains persistent. I think the main culprit is this fellow:</p>
  
  <pre><code>Shared Sub New()
      ' If the cart is not in the session, create one and put it there
      ' Otherwise, get it from the session
      If HttpContext.Current.Session("ASPNETShoppingCart") Is Nothing Then
          Instance = New ShoppingCart()
          Instance.Items = New List(Of CartItem)
          HttpContext.Current.Session("ASPNETShoppingCart") = Instance
      Else
          Instance = CType(HttpContext.Current.Session("ASPNETShoppingCart"), ShoppingCart)
      End If
  
  End Sub
  </code></pre>
  
  <p>Even when this session object remains persistent, it doesn't show up when I run a trace:</p>
  
  <p>e.g.</p>
  
  <p>my other sessions variables like Session("userId") show up in a trace while I NEVER see a value for Session("ASPNETShoppingCart") - which I thought was strange - perhaps this is because it is an object as opposed to a string?</p>
  
  <p>I've ruled out issues with the load balancer by bypassing it directly and calling the code directly on the server.</p>
  
  <p>I know the full session is not being destroyed when the issue occurs because I'm still logged in and other session values appear on screen - but my shopping cart values do not.</p>
  
  <p>Has anyone any idea as to what might cause something like this? Is there any way of protecting a session variable or at least being notified if it changes state so I can trace what's causing this?</p>