Archive for February, 2009

Type initializer exception for shared class

February 11, 2009

From time to time I have gotten a baffling error – “Type initializer exception was unhandled by user code”.

I had a shared (static) class that I use for some utilitity functions, and in that I have a shared constructor and a shared Init function to set a few of the shared variables. And this has run with no incident hundreds if not over a thousand times.

Background – this is a VStudio 2008 web site run from a file directory, not from IIS.

What I notice, however, is that when I run Visual Studio Debug, it may work once, but a second time , if there is a hanging instance of AspDotNet Development Server in my toolbar, then the second time I attempt to debug, my shared class will apparently not instantiate, and I get that weird message. I close Visual Studio, reopen, and then things work.

Also, in the past when I was running from IIS, I attempted to do the same thing (initialize a few variables in this shared class), but I attempted to do it in global.asax. I have since given up on that – the type initializer messages were too weird for me.