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 hitting Debug in Visual Studio, firing up Cassini and nothing happened.
This is to be expected as things like an Initialisation module, actually a lot of thing in 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 to that.
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). In this case the User Name 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.
My last tip is remember to compile. As you are now attaching through IIS and not Visual Studio, you have to remember to build your solution and check it builds, otherwise you could be looking at old code.