Can't break in global.asax / Application_Start

后端 未结 9 1152
名媛妹妹
名媛妹妹 2021-02-01 16:49

I got a break point on the first line of Application_Start(), but Visual Studio wont break on it.

Visual Studio have attached itself to the IIS working proc

相关标签:
9条回答
  • 2021-02-01 17:22

    My solution is to switch to using the 'Visual Studio Development Server' to deal with the application class (Global.asax) issues. When done I switch back to IIS.

    0 讨论(0)
  • 2021-02-01 17:32

    Reading your question, I assume you are using IIS for debugging, not Visual Studio Development Server.

    In this case, debugging application start is tricky, because it is only called once when the application pool is started or recycled. When Visual Studio attaches to the process, Application_Start has already been running.

    The trick is to make the application pool recycle without killing the process you are attached to.

    Do the following:

    1. In Visual Studio (must be run as Administrator) set your breakpoint in global.asax.cs and start debugging as usual (F5). The page opens in your web browser, but the breakpoint isn't hit.
    2. Now the trick: With a text editor, open web.config from where it is served by IIS, change it (e.g. enter a blank line somewhere) and save it. In contrast to recycling the application pool in IIS, this lets the application pool recycle (and thus running through Application_Start in global.asax.cs the next time the web site is called) without killing the process you are attached to.
    3. In your web browser, reload the page. The breakpoint should be hit now!

    That works for me (IIS 7.5, VS2015).

    0 讨论(0)
  • 2021-02-01 17:33

    I assume you're loading the application by clicking the "debug" button in Visual Studio? That's what I'm doing (in VS 2012) and seeing similar problems. Pressing that button the first time starts the application and correctly hits the breakpoint. But it seems like after I stop debugging the application itself keeps going. So, future attempts to debug just attach to the existing process.
    There's a "restart" button next to the "stop debugging" button, so I'd assume clicking that at least would change things. The debugging app does not show up in IIS manager, so I can't stop it there. Likewise, iisreset doesn't catch it either.

    Only thing I've figured out so far is to change a line of code, thereby forcing visual studio to trigger a build and then it kills the existing proc and starts over. Kind of annoying if I just want to step through there multiple times.

    I don't consider this a suitable "answer", but it might be a helpful workaround for you until somebody does come in with a real answer.

    0 讨论(0)
  • 2021-02-01 17:39

    Application_Start() only runs once, when the application starts. A few things that restart the application are:

    • web.config changes
    • recycling the worker process - you can do this in IIS Manager or by running iisreset at the command line.
    0 讨论(0)
  • 2021-02-01 17:40

    if [2092] w3wp.exe is a service that you made, try this : stop service -> rebuild service project -> start rebuilt service -> try to debug

    0 讨论(0)
  • 2021-02-01 17:42

    Whenever you run an application for the first time, or say start an application, there is an ASP.Net Development Server - Port [port number] that starts,

    Application_Start() runs once in the course of an application.

    If you want the break point to be reached , you have to stop the ASP.Net Development Server Port and run your application again.

    0 讨论(0)
提交回复
热议问题