global.asax breakpoint not hit

18,697

Solution 1

I've run into this same problem. I'm assuming you are using your local IIS instead of the VS Development Server. If this is the case, you won't be able to debug/Step through this code in the Global.asax.cs file because by the time the debugger has attached, this code has already executed in IIS. However, if you use the Dev server, you have the ability to get to this code as the debugger will already be attached.

So, Change the server in your project properties to use the Visual Studio Development Server.

This can be found by right clicking your project within Visual Studio > Project Properties > Web tab > Use Visual Studio Development Server.

Solution 2

What helped me was to add:

System.Diagnostics.Debugger.Break(); to that Application_Start() method.

Solution 3

On a project I was working on, the "Start external program" option was selected in the tab:

Visual Studio > Project Properties > Web

This was causing the breakpoints to not be hit.

Changing "Start Action" to "Current Page" fixed this problem for me.

Solution 4

As metioned in another answer, the Global.asax code will run before the debugger gets chance to attach. To get around that you can start debugging, then go into IIS and Stop/Start the Application which should then have the debugger pick up the restart.

Solution 5

If you do not want to use Visual Studio Development Server and use IIS, you can do the following (tested in VS2015 Professional)

1) Application pool in a running state - make sure that used Application Pool is running by accessing a page from your Web app (it may be stopped due to a Idle timeout).

2) Attach to process - Ctrl-Alt-P or Debug -> Attach to Process -> lookup your w3wp process that corresponds to your application.

Attach to process can be greatly sped up by using ReAttach extension which creates shortcuts to recently attached processes.

3) Application pool recycle - make sure that the application pool recycles by either entering IIS and recycling it or even faster, by entering web.config, making a no-effect change (put some blank outside of tags) and saving it

4) Start debugging - Perform a request to any page from your web application to force its initialization. You should be able to place and hit breakpoints from any of the startup methods in the Global.asax

protected void Application_Start()
void Application_BeginRequest(Object sender, EventArgs e)
protected void Session_Start(object sender, EventArgs e)
Share:
18,697
user259286
Author by

user259286

Updated on June 04, 2022

Comments

  • user259286
    user259286 about 2 years

    I have some code in my ASP.NET app in C# that's in the Global.asax.cs code file. In the Application_Start, Session_Start and Application_Begin Request I have set some breakpoints. However none of these are ever hit. I'm working on my local machine with VS8.

    Here's what I've tried:

    • Stopped the ASP Dev Server
    • Deleted all ASP.NET Temporary files
    • Created new Global.asax
    • Closing VS and opening back up
    • Clean and Rebuild project

    Upon trying my after these, the breakpoints will not hit.

    Any ideas why this might be?

    • Davide Piras
      Davide Piras over 12 years
      why don't you try to put some System.Diagnostics.Debug.WriteLine(...); in each of those events and check the output window while running the solution from Visual Studio? Do you see anything?
    • Shadow The Kid Wizard
      Shadow The Kid Wizard over 12 years
      See this answer - does it help in your case?
    • Andrew Barber
      Andrew Barber over 12 years
      Could you paste the actual declarations of these methods? Maybe you've declared them incorrectly.
    • user259286
      user259286 over 12 years
      Thanks for your quick replies. However none of those worked, even the stuff from the link. By the way, I am in Debug mode and I have breakpoints in page code-behind that do get hit.
    • Erix
      Erix over 12 years
      Try using IIs instead and see if they hit
    • stuartd
      stuartd about 9 years
      A bit late to the party, but if you edit the app.config file while the project is running, the breakpoint will be hit.
    • Zoomzoom
      Zoomzoom over 8 years
      Check this answer, worked for me: stackoverflow.com/a/8834890/1371217
  • Osel Miko Dřevorubec
    Osel Miko Dřevorubec almost 10 years
    In VS 2013 Visual Studio Development Server is missing , also @Ofiris answer doesnt help
  • QMaster
    QMaster over 6 years
    Not perfect but useful: entering web.config, making a no-effect change (put some blank outside of tags) and saving it. Deserved +1 :)
  • Eric Mutta
    Eric Mutta over 4 years
    In VS 2019 you follow the steps above but on the Web tab, look at the Servers section and choose IIS Express. I was using Local IIS and the breakpoints weren't working but switching to IIS Express fixed the problem.