A common problem for a .NET project is that after deploying a DLL into the bin folder or as a matter of fact doing any change in the bin folder of a web site all the data for any of the current in process sessions is lost. The same happens if we change the web.config file.

In some cases it makes sense to be so, for example if we store in the session objects that are being changed by the current deployment of a new DLL. But in many other cases it is rather a disturbing frustrating side effect that we want to avoid.

We have to be able to deploy new modules and components into an existing web side without interrupting the experience of our current users. There are several methods proposed as a resolution for that problem. One is to use Sql Server based sessions, and another to use State Server. Even though all those solutions will solve your problem, you will have to pay for that with a significantly decreased performance. None of the other session modes is as good performing, as in process sessions.

The tool I want to present here aims to resolve this problem for the in process sessions, and here is how it works. Normally the tool is disabled, but just before you deploy a DLL file or change the web.config you enable it by setting a value of a singleton object. This will create a CurrentLog.log file in a specially designated folder with write permissions for the IIS user, by default the folder is /App_Data/SavedSessions. The existence of that log is indication that the tool is enabled. Any ending session then will be saved in the same folder as a serialized data in a separate file. After you are done with the deployment of the DLL or web.config changes. You set the same singleton as disabled. After that any session that was previously saved will be restored, and the log is renamed with the date of disabling it and the session ID in it’s name. Each enabling/disabling created a separate log file. Up to 10 log are being kept, but you can change that number

The tool is built with the Unity Framework and consists of 5 replaceable blocks Configuration, Logger, Storage Manager, Serializer, and the Main Manager.

The limitations of the tool are as follow:

  1. It uses .NET 3.5+ framework
  2. Requires a folder with write permission for the IIS user
  3. Any object that is stored in the session must be serializable

Here are the common steps to set it up:

  1. Take the three Unity Framework DLLs Microsoft.Practices.ObjectBuilder2.dll, Microsoft.Practices.Unity.dll and Microsoft.Practices.Unity.Configuration.dll as well as the project DLL com.bodurov.SessionRecovery.dll and put it in you bin folder
  2. Change your Global.asax to include these calls
    <%@ Application Language="C#" %>
    <script runat="server">
        protected void Session_Start(object sender, EventArgs e)
        protected void Session_End(object sender, EventArgs e)
  3. Now somewhere in the backend of your site create a button or a control that will enable / disable the session recovery tool. The code should look like this:
    com.bodurov.SessionRecovery.Sessions.Manager.IsEnabled = true;
    or to disable it
    com.bodurov.SessionRecovery.Sessions.Manager.IsEnabled = false;
    You can also use SessRec.aspx from the demo web application in the source code if you don't yet have backend. Just don't forget to change the password.
  4. Create a folder /App_Data/SavedSessions and give it write permission for the IIS user.

That is all that needs to be done to make it work. Just remember to enable the control before changing the bin folder / web.config and disable it after that, otherwise any ending session will be saved.

In the source code you can find a demo web application project.

If you want to replace some of the modules, you can do that by changing the Unity Framework configuration settings for example. If you want to create your own serialization module you could add this code to your configuration file

    <section name="unity"
                 Microsoft.Practices.Unity.Configuration" />
      <typeAlias alias="singleton" 
                  Microsoft.Practices.Unity" />
      <container name="SessionRecovery">
          <type type="com.bodurov.SessionRecovery.Interfaces.ISessionRecoverySerializer, 
                mapTo="YourNamespace.YourSerializerClass, YourAssembly">
            <lifetime type="singleton" />

In this example YourNamespace.YourSerializerClass class that implements com.bodurov.SessionRecovery.Interfaces.IsessionRecoverySerializer located in YourAssembly.dll is set to replace the default serializer.

The full source code is available at http://sessionrecovery.codeplex.com/

Share this post:   digg     Stumble Upon     del.icio.us     E-mail

Commenting temporarily disabled