ASP.NET VERSIONS: 1.0 | 1.1
Web Farm Warnings
Dangerous Defaults and Custom Types
Editor's Note: This is a companion tip to Deal With View State Issues published in the March 2004 issue of asp.netPRO.
By Dino Esposito
If your ASP.NET applications are running on a network of Web servers, i.e. a Web Farm, you must ensure that all machines have a common value for the validationKey attribute of the <machineKey> entry in the machine.config file. In a Web Farm, each client request can go to a different machine on every postback. If you leave the validationKey set to AutoGenerate (its default setting) you can't be certain that all the machines will auto-generate the same key. Instead, you must set it to a fixed string that's common to all the machines on the Web Farm. This guarantees that any machine can successfully handle the view state generated by another machine.
Another aspect of view state that gets tricky, from a Web Farm perspective, involves the storage of dynamically generated types. If your application happens to use a type that isn't stored in a precompiled assembly, or one that is stored in an assembly with a dynamically generated name, you can run into serious trouble when you try to retrieve the type across a Web Farm.
When a variable of this type is placed into the view state it is binary serialized unless a type converter has been defined. The binary serializer stores the (dynamically generated) name of the assembly in the payload. As a result, when another machine attempts to deserialize that view state it will likely lack the dynamically created assembly. The best way to avoid this problem is to not use custom types, especially if they're created dynamically. Use simple types to get better performance, and to avoid these problems related to Web Farms. If you really need to use custom types, use precompiled assemblies and deploy them to all machines in the Web Farm.
Dino Esposito is a trainer and consultant who specializes in ASP.NET, ADO.NET, and XML. The author of Building Web Solutions with ASP.NET and ADO.NET and Programming Microsoft ASP.NET (both from Microsoft Press), Dino is also a co-founder of http://www.VB2TheMax.com. E-mail him at mailto:[email protected].