Ok, it’s been a while…
So, I have an application that runs as the windows shell (this is a locked-down PC, controlling a machine in a factory). Like all good applications, it has a number of settings that need to persist between sessions so I save them (using XmlSerializer) when the app closes. This is all hunky-dory in my test setup, ie. when my app is not the shell.
So then I deploy to a real test machine, running as the shell – but on shutdown this doesn’t seem to work, I get a FileNotFoundException when creating an XmlSerializer. Huh?
After a bit of digging, the reason is quite logical (although not immediately obvious) – XmlSerializer generates assemblies dynamically to perform the serialization, and these go into your temp directory. However, Windows purges these directories while shutting down, hence the assemblies disappear (or maybe don’t even get created, I’m not entirely sure), hence FileNotFound.
My workaround is to instantiate the XmlSerializer when I first load my settings and keep it hanging around. That way the assembly gets generated at an appropriate time, and loaded into memory, so when it comes to shutdown time there is no further code generation required. Simples.