A Persistent Application State
We all know that an application state lasts while the application is running, but if the AppDomain gets restarted, from something like a change in the web.config file or a recompile. What if we want an Application State that always persists? One that is never lost, even if the entire server is restarted? Well I set off to do exactly that, and here is what I got.
The first thing I had to do when deciding how this would work is how I would store the values for this state. Logically a SQL Server came to mind, so I had to build an underlying way of communicating with a SQL Server, and an HttpModule seemed like the best way to accomplish this. So I was going to use SQL Server for holding my values. The next task is
how
to store everything in the SQL Server. I came up with 3 fields. The first being the key, the second being the application name (that way if you have two applications using the same key names, they won't conflict) and the 3rd being the value. I decided to go with Binary Serialization for storing values, to the value field is VARBINARY(MAX). This way we can store
anything
in our Persistent Application State so long as the object is serializable. To keep performance up, it doesn't read and write from the SQL Server exclusively. It caches everything for reading, so when you get a value it is getting it from an underlying dictionary, and during an update it updates the cache and the SQL Server values. The only time a read is done on the SQL Server is when the Init() method is called on the HttpModule. A SQL Script for creating the table is included in the project.
Since we are using an HttpModule, the module must be correctly registered in the web.config file, which would look something like this:
The next thing I had to accomplish was making this thing configurable. The web.config file seemed like the best way to do this, so I made a quick ConfigurationSection class to handle the configuration. The configuration has 2 basic attributes.
applicationName
and
connectionStringName
. applicationName should be a string that uniquely identifies the application. The connectionStringName is the name of the connection string that should be used to connect to the SQL Server that is in the section. An example of the web.config is included in the project.
The last thing I had to deal with is how to make this accessable from the Page. To solve this, I created a BasePage that exposes a Dictionary that works exactly like the Application, and hooks into the Module. To use it, derrive all of your pages from the class KGJ.PersistentApplicationState.BasePage and set the web.config file to use that as the base as well. This will expose the Dictionary PersistentApplication. From there on, use it like the Application State, except they will always be there until you delete them. How to use it: Simply put the compiled DLL in your BIN folder.
As far as other Databases, this code is easily adoptable to an Oracle or MySql client. Eventually I will adopt it so that it works like a provider.
Download the source code and projects here:
Persistent Application State
Feel free to leave comments and suggestions, there is always room for improvement.