Tuesday, April 21, 2015

Getting a breakpoint in Global.asax to be hit

When using IIS for debugging, not Visual Studio Development Server.

In this case, debugging application start is tricky, because it is only called once when the application pool is started or recycled. When Visual Studio attaches to the process, Application_Start has already been running.
The trick is to make the application pool recycle without killing the process you are attached to.
Do the following:
  1. In Visual Studio (must be run as Administrator) set your breakpoint in global.asax.cs and start debugging as usual (F5). The page opens in your web browser, but the breakpoint isn't hit.
  2. Now the trick: In Visual Studio, go to web.config, change it (e.g. enter a blank line somewhere) and save it. In contrast to recycling the application pool in IIS, this lets the application pool recycle (and thus running through Application_Start in global.asax.cs the next time the web site is called) without killing the process you are attached to.
  3. In your web browser, reload the page. The breakpoint should be hit now!
That works for me (IIS 7.5, VS2010) and (IIS 8.5, VS2013)