I overheard a colleague complaining about how he had wasted his whole afternoon tediously trying to debug an issue we had in an InitializationModule.  We had a standard MVC web project and he was trying to run his mode within Visual Studio debug mode.

This is to be expected as things like an Initialisation module, actually, a lot of things within App_Code, are triggered by the application server.  

If you have the same problem and you're struggling to debug your code, then the solution is actually pretty simple.

First, you will need to make sure you have your site working in IIS,  i.e. you have a website that points to your webroot and the site WORKS!  

Trust me, from hours of wasted time, this part is very easy to do. In Visual Studio instead of clicking the play button go to the Debug Menu and then click 'Attach To Process'.  From here the 'Attach To Process' dialogue should appear.

First thing, make sure 'Show Processes from all users is selected.  In the list, you want to find the IIS process for your website and attach it to the process

Tip: If you cannot see your IIS process, called w3wp.exe than open your browser of choice, and load your website up, the process should now magically appear. You may see more than one IIS entry (if you have more than one website configured in IIS).  

The username should help you figure out the one you care about (this also might be the time you want to split your application pools to use their own user ) )

After selecting the process, hit attach and off you go.   There are some things you need to remember.   When you want to rerun code in an InitializationModule, you will have to hit the stop/start button in IIS to run the code again (not Visual Studio).  Do not do an IISRESET as this will kill the process.  By using the start/stop IIS feature you keep hitting your code without having to re-attach your process each time.  

The last tip which seems obvious but it is easy to forget is to remember to compile and make sure it completes successfully.  As you are now attaching the debugger via IIS you have to remember to build the solution, otherwise, you could be looking at old code.