A multi-request-safe ViewState Persister
Mark Miller has posted his code for a ViewStatePersister using the "common sense but not obvious" GUID technique that was outlined previously by Scott Mitchell and myself.
He stores a GUID in the ViewState hidden field, and sticks the bloated ViewState in a temp file on the server. It doesn't solve the problem when running multiple web servers while using stateless balancing (meaning: NOT using sticky sessions/node affinity) but it's the most elegant and complete solution I've seen yet and should work great on a single web server.
A few questions I have though:
- When do the files get cleaned up and how often? Do you clean up old ones in a background thread within ASP.NET or a separate Windows Service? Thought: I wonder if you could delete them after immediately after the Load? You wouldn't be able to RE-post data, but it'd be cleaner, no?
- GUID generation is very expensive, and can really slow you down under load. I wonder if it would be faster/easier to have a single long and use InterlockedIncrement or InterlockedIncrement64 to safely increase the value on each call until it overflows and you start again at 0.
Many thanks to Mark for sharing!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.



About Newsletter