52

I can't debug global.asax file!

I have some codes in Application_Start() method but when I set a break point in the method, it is ignored!

Is this normal?

Hasib Tarafder
  • 5,773
  • 3
  • 30
  • 44
Mehdi
  • 5,435
  • 6
  • 37
  • 57

11 Answers11

94

A simple way to break in Application_Start() is to use the System.Diagnostics.Debugger class. You can force the application to break by inserting System.Diagnostics.Debugger.Break() where you would like the debugger to break.

void Application_Start(object sender, EventArgs e) 
{
     System.Diagnostics.Debugger.Break();

     // ...
}
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Patrick Lee Scott
  • 8,217
  • 3
  • 36
  • 42
78
  1. Attach the debugger to the IIS process.
  2. Open the global.asax file and put in a breakpoint.
  3. Add a space to the web.config file and save the file (this causes the current web application to reset);
  4. Refresh / goto a web page on the site.
  5. watch in amazement when the debugger stops at your breakpoint. :)
John Kelly
  • 781
  • 5
  • 2
  • 1
    This is also known as 'Touching the web.config' and can be used for several scenarios when you need to refresh your site without touching IIS. Can't believe it didn't occur to me. Thanks. – JsAndDotNet Oct 09 '12 at 08:16
  • I wish it was that simple. But for me? "computer says no" – Justin Aug 07 '15 at 01:10
  • 1
    Worked perfectly. When performing step 1 in Visual Studio I had to tick "Show processes from all users" and attach to the IIS process called "w3wp.exe". – Fordy Aug 24 '18 at 08:45
8

Application_Start() is invoked once per AppDomain. If you're not hitting your breakpoint, it means the AppDomain was already created, so do the following:

  • In your quick-launch bar, there is an icon for the VS web server (its the one which says "Local Host Some Port"). Right click and choose "Stop" or "Close". This should kill the AppDomain.
    • If you're using IIS, you need to restart your site manually.
    • Alternatively, modifying the web config or Global.asax file is usually enough to restart the AppDomain.
  • Restart your debugging, you should hit your breakpoints now.
Juliet
  • 80,494
  • 45
  • 196
  • 228
7

Check that your web application is in debug mode (<compilation debug="true"> in web.config).

If you're using developer's IIS started by VS, just restart it or rebuild application.

If you're on normal IIS you have two options:

  1. For web-site is configured to work with development folder (where you VS web project is deployed) you just have to restart application pool set for that web-site and start debugging before first request reaches server (you can always restart app pool during debug).
  2. For web-site that works on another folder or even on remote server you have to attach to the process. To do this you need remote debugger installed on remote machine or your own (depends on web-server location) and use Debug - Attach to process menu, enter computer name and then select a process to debug. It is usually a w3wp.exe working in managed mode type.
terR0Q
  • 1,369
  • 8
  • 17
5

Yes, it is normal.

Application_Start() is processed by IIS.

But all other methods, for example Session_Start, and all others, except Application_Start() can be debugged normally.

SteveC
  • 15,808
  • 23
  • 102
  • 173
user224564
  • 1,313
  • 1
  • 10
  • 14
5

Maybe you should try:

  • stopping development server in taskbar
  • switching the configuration from release to debug
nandokakimoto
  • 1,361
  • 7
  • 14
  • 3
    It's got a minus rating because it's wrong. It's correct to say that you need to be in debug (or else you don't have the pdb files) and you need to capture the server when it starts (because this is when that code is ran) but you won't be able to attach to the process. You need a running process to attach the debugger, the global.asax code is ran in the first few miliseconds of this process starting so it's literally impossible (without using the [correct answer below](https://stackoverflow.com/a/4809464/542251)) to attach to the process as it runs this code – Liam Feb 28 '18 at 11:53
3

Another alternative to the accepted System.Diagnostics.Debugger.Break(); would be

void Application_Start(object sender, EventArgs e) 
{
   System.Diagnostics.Debugger.Launch();
   //...
}

which shouldn't break the code and should start the debugger even if the service were started with different rights.

VladL
  • 12,769
  • 10
  • 63
  • 83
2

For me, my debug breakpoint already executed in IIS by the time the debugger is attached. So the solution was to alter global.asax with a little space and save the file. After refresh, my breakpoint is now hit.

Solution here: https://wakeupandcode.com/hitting-breakpoints-in-global-asax/

Hugo A.
  • 218
  • 2
  • 12
  • Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – baduker Apr 10 '19 at 16:45
  • 1
    Thank you! This was the only solution that worked for me. Add a space inside the `Application` tag, hit save, and then refresh the browser. – PerpetualStudent Dec 03 '19 at 19:25
2

Delete the global.asax and add a new one. In my solution, there has been a global.asax and a global.asax.cs.

All the methods (Session_Start, Application_Start, ...) have been in the bot files, but only the ones in the global.asax have been considered. So, break points and code in the cs don't do anything.
Only after recreating the file, the global.asax.cs had the appropriate methods and they ran.

eeerahul
  • 1,629
  • 4
  • 27
  • 38
adonis404
  • 21
  • 1
  • I followed the instructions here: http://rossnelson.blogspot.ca/2005/11/fixing-globalasax-in-aspnet-20.html. Basically, remove the global.asax and add a global application class which recreate the asax with a code behind file which I can then step into. I am running on IIS and it works fine. – cbeuker Aug 28 '12 at 18:01
0

Dont expect the Application_Start() function to be called immediately by pressing f5. Application_Start() is invoked only at the time of first request to the application is made. Strange but true.

Kumaresan Lc
  • 281
  • 1
  • 2
  • 8
0

In case all of answers not working, try:

<compilation debug="true" ... />

in web.config. ;)