Wednesday, December 12, 2007

Workaround: Debugging Global.aspx.cs with ASP.Net Web Server within Visual Studio

 

When you add a global.asax file to your "Web Application Projects (WAPs like below:

image

and then write code in Application_Start event and try to debug it like below using Visual Studio inbuilt Web Server

image 

Then during your first debug the breakpoint is hit but during your subsequent debug runs the breakpoint is not hit.

The reason behind this is that we do not kill the ASP.Net Web Server process after your every debug run and hence Application_Start() is not fired every time.  There is a good reason why we do so...  Starting ASP.Net Web Server process is an expensive task and in most of the scenarios recycling this process after every debug would adversely impact your performance...  If you do not want to debug your Application_Start() method then probably you do not need to have the process restart and save performance most of the time... 

However if you would like to have the breakpoint hit you have few easy workarounds... One of the workaround is that you stop the web server from your system tray as in the figure below:

image

Alternatively you can go to your property pages of your web application and enable Edit & Continue like shown below:

image

When you choose Edit and Continue then we recycle the ASP.Net Web Server process on every debug run (it is needed for the Edit & Continue functionality to work)...  This way although you will see very marginal degrade in your performance you will still be able to debug your Application_Start() methods...

Hope this helps...

2 comments:

Anonymous said...

Hi,
Not sure if you are still active on this blog. But I have a problem which very much relates to this blog post.
I have a global.asax file in my web service project. I have placed some code in the Application_Start and Session_Start events.

Now when I try to put a breakpoint in these two events and try to debug, I do not find my debug step breaking into these lines.

Instead it directly breaks into another class file method.
Is there something else that I need to take care of.
Thanks,
zullu

Vishal R Joshi said...

zullu,
are the workarounds not working for you? Are you using file-->new website or file-->new project-->web
Vishal