Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic field initializer runs before Application_start, sometimes
    primarykey
    data
    text
    <p>I've got an ASP.NET web app that is starting to show some very strange behavior. Here's some example code:</p> <pre><code>// in Bar.cs public class Bar { public static Baz baz = Something.Step2(); } // in Global.asax public void Application_Start(...) { Something.Step1(); } </code></pre> <p>The short version of the story is this: On some machines, Something.Step2 is executed before Something.Step1 and is throwing an unhandleable exception. On other machines, Step1 correctly executes before Step2. Global.asax and all the objects it uses do not refer to Bar at all.</p> <p>When are static fields supposed to execute in relation to other programming elements? Why would two machines (both Win7 64-bit, both with .NET 4.0, same IIS version, etc) execute things in different orders? The order is consistent on each machine too. On my machine, it <em>always</em> executes Step2 before Step1, but on my coworker's machine it <em>always</em> executes Step1 before Step2.</p> <p>Help very much appreciated.</p> <p><strong>Update</strong> I've found the root cause why my static field is being accessed. Class "Bar" from my example is actually a custom authentication module, and is referenced in web.config as the Authentication handler under System.webServer. If I remove that line from web.config, my system calls Step1 first and never calls Step2 at all. My question changes subtly to: "Why does web.config cause my static initializers to fire, and why does it cause them to fire before Application_Start executes?"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload