TempData not working with SQL Server Session State and ASP.NET MVC Preview 3#

We are extensively using ASP.NET MVC Preview 3 for one of our web projects and we are loving it.  The other day when we deployed our app to our web farm, we realized that we got an error when typing in a wrong password and that our app was not returning the handled error of improper login and the app was blowing up with the error, "Unable to serialize the session state."  When we tracked it down we realized that this only happened when using TempData instead of ViewData. 

So, when redirecting to an action in MVC, you will lose anything you have in ViewData so if you want to carry an error to another action and show it in the next view that is rendered, then you will need to create your own session variables or use the built in TempData which works great except when you are using SQL server for session state instead of in-proc.  Well we have no choice than to use SQL server for session state since we have several web servers on our web farm. 

So, since ASP.NET MVC source code is available on Codeplex, I decided to investigate what class makes up TempData.  I found that TempData uses an internal class called Pair and it is not marked as serializable.  Dare I try to fix it and run with our own version of System.Web.Mvc?  Why not, it is just C#?  the fix was very easy and after adding the serializable attribute to the Pair class.  Pair.cs found in the folder Util inside of the MVC project.

namespace System.Web.Util {
    using System;

    // Generic Pair class.  Overrides Equals() and GetHashCode(), so it can be used as a dictionary key.
    [Serializable] 
    internal sealed class Pair<TFirst, TSecond> {

 

All the tests now passed and most importantly, our site works perfectly.  It was very empowering to add a simple change to the framework and not have to wait for a fix from Microsoft.  I feel so much more comfortable using frameworks that have the source code with them.  That is why we use ASP.NET MVC and Subsonic 2.1.

Friday, June 27, 2008 7:21:40 AM UTC #    Comments [0]  |  Trackback

 

Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, i, strong, u)  

Enter the code shown (prevents robots):

 

All content © 2008, Jim Zimmerman
Book
New Book
Links to me
On this page
Sponsors
Calendar
<June 2008>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Archives
Tags
Blogroll OPML
Technorati
Favorite Links
Disclaimer

Powered by: newtelligence dasBlog 1.9.6264.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail