Can Visual Studio restart IIS Express on new debug

2019-01-22 14:13发布

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.

2条回答
Deceive 欺骗
2楼-- · 2019-01-22 14:33

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...

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-22 14:34

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:

enter image description here

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.

查看更多
登录 后发表回答