In this tutorial, you will learn the optimal way to debug an Episerver scheduled job or initialization module. Recently, 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 some hit a breakpoint within
debug mode unsuccessfully 🛑🛑🛑
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. You need a hostname and a website. This site should point to your webroot and more importantly, it needs to work! (Trust me, from many wasted hours of my life, this part is very easy to forget).
In Visual Studio, instead of clicking the
play button (
F5), go to the Debug Menu and then click
Attach To Process. From here the
Attach To Process dialogue should appear. Make sure
Show Processes from all users is selected. In the list, you want to find the IIS process for your website and attach the debugger to the process. This process should be called
Tip: If you cannot see your IIS process, called
w3wp.exe, 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). If that happens, the username should help you figure out which
w3w.exe process you care about. After selecting the process, click
attach and off you go. Your breakpoint should now hit!
There are some things you need to remember when doing this. First, when you want to make changes to your
InitializationModule, you will have to hit the stop debugging, re-build, then re-attach. Do not do an IISRESET as this will kill the process. By using the start/stop IIS feature you will minimise the number of re-attaches you need to do.
This last tip may seem obvious, however, is easy to forget, is to make sure you compile the code and make sure it completes successfully before re-testing. 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. Happy Coding 🤘