I have an ASP.NET web application that is starting to show very strange behavior. Here is a sample code:
// in Bar.cs public class Bar { public static Baz baz = Something.Step2(); } // in Global.asax public void Application_Start(...) { Something.Step1(); }
The short version of the story is this: on some machines, Something.Step2 runs before Something.Step1 and throws an inexplicable exception. On other machines, Step1 is correctly executed before stage 2. Global.asax and all objects that it uses do not belong to Bar at all.
When should static fields be implemented in relation to other programming elements? Why do two machines (both Win7 64-bit and .NET 4.0, the same version of IIS, etc.) do things on different orders? The order is also agreed on each machine. On my machine, he always runs Step2 to Step1, but on my machine, the co-author always runs Step1 to Step2.
Help really appreciate.
Update . I found the reason why my static field is being accessed. The Bar class from my example is actually a custom authentication module and is referred to in web.config as the authentication handler in System.webServer. If I remove this line from web.config, my system will call Step1 first and never call Step2 at all. My question subtly changes: "Why does web.config cause my static initializers to start and why does it start them before running Application_Start?"
source share