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.