Is it possible to debug Global.asax?

2019-01-16 11:22发布

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?

10条回答
爷的心禁止访问
2楼-- · 2019-01-16 11:24
  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. :)
查看更多
成全新的幸福
3楼-- · 2019-01-16 11:28

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.

查看更多
Melony?
4楼-- · 2019-01-16 11:29

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.

查看更多
贼婆χ
5楼-- · 2019-01-16 11:35

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.

查看更多
一夜七次
6楼-- · 2019-01-16 11:35

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.

查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-16 11:37

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.
查看更多
登录 后发表回答