IHttpModule Gotchas @dominicpettifer.co.uk

I came across this blog post about the Init() method of an HttpModule being called more than once. The solution is simple: create a boolean flag to indicate whether or not the app is started and an empty object to lock. Then, in the Init() method, check the flag, lock the object, then check the flag again before performing any application code.

For example:

private static bool HasAppStarted = false; 
private readonly static object _syncObject = new object(); 
 
public void Init(HttpApplication context) 
{ 
    if (!HasAppStarted) 
    { 
        lock (_syncObject) 
        { 
            if (!HasAppStarted) 
            { 
                // Run application StartUp code here 
 
                HasAppStarted = true; 
            } 
        } 
    } 
}

The full blog post can be viewed here.

I was directed to the above post via The Morning Brew.