I have an ASP.Net application that performs some database initialization during Application_Start in Global.asax. I’m using IIS Express to debug the application.
I find that if I stop debugging, clear the database, and restart again, the Application_Start code does not get called and my database is not initialized properly, so my application fails. This is because the IIS Express instance is still running after debugging stops. Rather than restarting it, Visual Studio appears to be attaching to the existing process.
Is there a way to change this behavior such that starting a new debugging session always restarts the application process in IIS Express (or resets the application pool in regular IIS)?
Alternatively, is there a way to force IIS Express to shut down when the debugging session ends?
I did find that if I check “Enable Edit and Continue” from the Web dialog in the project settings, that it has the side effect of stopping IIS Express when debugging ends. But I’m not sure I want to introduce the side-effects of Edit and Continue just for the purposes of stopping IIS Express. Surely there’s a better way?
Not sure if it matters, but this is VS2012.
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Unfortunately it’s not achievable through configuration. I may propose you another trick, but it’s up to you to decide whether it’s better than yours or not 🙂
You may create a post-build event which changes the timestamp of a web.config file. I used a touch.exe tool from http://www.stevemiller.net/apps/. You also need to set the “Run the post-build event” to Always. So your “Build Events” configuration may look as follows:
With this option set anytime you start the debugger, web.config timestamp is getting updated causing application restart (application appdomain reload) on the first request – but at this point you are already attached to this process so your
Application_ event breakpoints should work.
You can use a Powershell one-liner to avoid relying on an external app:
powershell -NoProfile -Command (Get-Item "<path>Web.config").lastwritetime=(Get-Date)
Original inspiration comes from here…