Customers using Jetfire Workflows rely on websites with good connectivity to stay connected to their business and keep their business processes up-to-date. Recently, our ISP moved their Data Center to be better served by a higher speed internet connection. Web pages are definitely served faster, but I also noticed busy intervals during the day leading to page rendering delays and often dropped logins for customers. The dropped logins require users to re-login – a nuisance when you are busy.
Session State in the web.config file is set up as ‘InProc’, meaning that the Session State is stored in memory. By changing Session State to ‘SqlServer’, the goal was to avoid unnecessary re-logins.
In a hosted environment, this procedure is:
- Run the sql script for the ASPState database on the local server
- Change the Session State parameter in web.config to ‘SqlServer’
- Set the parameters for the SqlServer Session State
In an ISP environment, step 1 is changed to create the database and upload the database schema.
For our lazy deployment person (that’s me), even this simple procedure was too complicated. I needed a one-click procedure to
manage the transition automatically and minimize the amount of effort required to convert each site.
These are the design steps used to add the session state tables to an existing database and then create a Web Control to change the session state.
- Create the ASPState tables in my local database
- Create a script for the ASPState tables
- Put a C# code wrapper around the ASPState sql script
- Create a Web Control that changes Session State with one-click
The figure on the right shows the desired end result – a web control that changes Session State from memory storage to database storage and simple enough that the customer administrator can do it.
ASPState Tables
The .NET Framework includes an install database and tables script for ASP State. Since we are adding the ASPState tables to an existing database, we need to create an sql script that only addresses the tables (and stored procedures).
This involves installing the ASPState tables in my development system. The sql script, InstallSqlState.sql, to do this is found in one of the following folders; system drive\ Windows\ Microsoft.NET\ Framework\version\.
SQL Script for ASPState Tables
Once the ASPState database and tables are created, generate the script for the ASPState tables. I used the Database Publishing Wizard found in codeplex project: http://sqlhost.codeplex.com. Just follow the instructions.
C# code wrapper about sql script
The sql script is not a slam dunk at this point. There are a few gotchas that need to be addressed.
- The sql script does not contain the name of the customer’s database. When ISP hosting is used, it is highly likely that every database that the ASPState tables are added to has a different name. Therefore, a name, such as ‘DatabaseNamePlaceHolder’ is embedded in the file in multiple places. (there were over 80 places where this occurs)
- Delete the GO statements. Do a replace all ‘GO’ strings with ‘’ (an empty string).
- strings in the file, e.g. “2” need to be converted to “”2””. [there was only one instance to change.]
Code Snippet 1: SQL Script for ASPState tables
The above code snippet shows a portion of the SQL script for installing ASPState tables.
Note: The sql script is over 1100 lines of code, which is why it is not included in its entirety.
Code Snippet 2: Create the ASPState Tables
The above code snippet creates the tables for the ASPState tables.
- Get the Connection String where the tables will be added.
- Instantiate an SqlConnection object. This is used to get the name of the customer’s Database.
- Get the sql script and replace ‘DatabaseNamePlaceHolder’ with the name of the customer’s Database.
- Execute the modified script on the database.
The end result is tables: ASPStateTempApplications and ASPStateTempSessions, along with all Stored Procedures are installed in the target database.
Web Control that changes Session State with one-click
This section addresses how to change the mode for Session State programmatically. (The user interface code is straight-forward.) The Session State is located in the web.config file at the root of the website. It is retrieved using the code found in Code Snippet 3.
this.config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/");
SystemWebSectionGroup webGroup = this.config.GetSectionGroup("system.web") as SystemWebSectionGroup;
this.sessionState = webGroup.SessionState;
Code Snippet 3: Get the Session State from web.config
The ‘ChangeMode’ method, shown in Code Snippet 4, is called from the link click event of the Web Control. Key points include:
- Only SQLServer and InProc modes are supported
- The session State object is updated for the selected mode
- A message is printed to the user providing a positive indication of what has changed
- The change is saved in the web.config file.
- Text displayed on the Web Control is updated.
Code Snippet 4: Change the Mode of the Session State
There is a lot of flexibility in the SQLServer mode for Session State. Read the MSDN help closely to ensure that you engineer the correct operation for your application.
When the customer Administrator converts the storage of Session State from memory, ‘InProc’ to database, ‘SQLServer’, all users need to re-login because when the web.config is changed for this procedure and of course, because the Session State for each user is now null.
Object Serialization Gotchas
Further web testing showed that my work was not finished. There was one object that was stored correctly when the Session State Mode was ‘InProc’. However, it was real clear that the object needed to be serialized to the database when the Session State was changed to ‘SQLServer’. Whereas, ‘InProc’ stores a reference to the object in the Session memory, ‘SQLServer’ serializes the complete object to the database for each Session.
This required some re-work of the class in question and the code that used the object.
0 comments:
Post a Comment