title: c# .net in-memory persistence
body: <p>I would like to have a "user message" available for <em>every</em> request sent back by the server. If there is not a user message, the message goes back blank. If there is one, an icon is activated on each user screen <em>after their request</em> is completed.</p>
  
  <p>[edit]
  The "user message" is something that is being set by an administrator for the application I'm deploying. The administrator can enter text into a field and click a button to send this message to every other user of the system. Any time another user performs any kind of action, the current user message is attached to the JSON response and handled by the front end.</p>
  
  <p>In order to optimize this, I want the message to be stored <em>in memory</em> (not in the database).</p>
  
  <p>I have tried to use static. I have tried to use HttpApplicationState. In both cases, the value of the user message is "blanked out" after some period of time. After some research, it appears that both statics and HttpApplicationState are subject to <em>IIS</em> and when it decides to recycle the application pool. (or some such)</p>
  
  <p>This volatility of a <em>static</em> is mysterious: it should be <em>static</em> - so long as <em>IIS</em> itself lives, this variable should live. It should not be dependent on some kind of "reset" or whatever. The HttpApplicationState is some other situation that I don't fully understand.</p>
  
  <p>I would like a way to store a value in a non-volatile variable that I can rely on. If I set this value today, it should be there tomorrow, or next week, so long as <em>IIS</em> is not stopped and restarted.</p>
  
  <p>Any help?</p>
  
  <hr>
  
  <p>here is what i have done to solve the problem as per the accepted answer below:</p>
  
  <ol>
  <li>the user message is a sometime thing. so when the message gets set by some administrator, store the response in the database at that point in time and store it in the Application["UserMessage"] object.</li>
  <li>when round-trips from users come in, the in-memory text for the user message gets added to the json return value.</li>
  <li>the message can be cleared by the administrator at any time, which clears both the in-memory message and the database field.</li>
  <li>when IIS decides enough is enough and recycles the application, the Application_Start() method (among other tasks) will also re-seed the user message from the database value that was stored when the user message was set (as per step 1).</li>
  </ol>
  
  <p>now the application works as expected. no extra price is paid going to the database for every user request into the system - the user message always comes from memory. in addition to this, the database is updated or loaded for the user message very few times.</p>