C# Application_Start 没有触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application_Start not firing?
提问by Bob Yexley
I have an ASP.NET MVC (beta) application that I'm working on, and am having trouble figuring out if I'm doing something wrong, or if my Application_Start
method in Global.asax.cs is in fact not firing when I try to debug the application.
我有一个正在开发的 ASP.NET MVC(测试版)应用程序,但我无法弄清楚我是否做错了什么,或者我Application_Start
在 Global.asax.cs 中的方法在我尝试时实际上没有触发调试应用程序。
I put a breakpoint on a line in my Application_Start
method, and am expecting that when I attempt to debug the application that the breakpoint should get hit... but it never does. Not after I reset IIS, not after I reboot, not ever. Am I missing something? Why is this method never getting called?
我在我的Application_Start
方法中的一行上放置了一个断点,并期望当我尝试调试应用程序时,断点应该被击中......但它从来没有。不是在我重置 IIS 之后,不是在我重新启动之后,永远都不是。我错过了什么吗?为什么这个方法永远不会被调用?
采纳答案by Gregory A Beamer
If this is in IIS, the app can get started before the debugger has attached. If so, I am not sure if you can thread sleep long enough to get attached.
如果这是在 IIS 中,应用程序可以在调试器连接之前启动。如果是这样,我不确定您是否可以线程睡眠足够长的时间来连接。
In Visual Studio, you can attach the debugger to a process. You do this by clicking Debug >> Attach to process. Attach to the browser and then hit your application. To be safe, then restart IIS and hit the site. I am not 100% convinced this will solve the problem, but it will do much better than firing off a thread sleep in App_Start.
在 Visual Studio 中,您可以将调试器附加到进程。您可以通过单击调试 >> 附加到进程来执行此操作。附加到浏览器,然后点击您的应用程序。为了安全起见,然后重新启动 IIS 并访问该站点。我不是 100% 相信这会解决问题,但它比在 App_Start 中触发线程睡眠要好得多。
Another option is temporarily host in the built in web server until you finish debugging application start.
另一种选择是临时托管在内置 Web 服务器中,直到您完成调试应用程序启动。
回答by ninj
I think the application start event only gets fired when the first request is made, are you hitting your website (i.e. making a request)?
我认为应用程序启动事件仅在发出第一个请求时才会被触发,您是否访问了您的网站(即发出请求)?
回答by Rex M
When you say "debug", do you mean actually launching the application from Visual Studio's built-in webserver for debugging, or do you mean attaching to the process in IIS? If it's the former, you should hit Application_Start, but if it's the latter, it can be difficult to be on the process early enough to catch it.
当您说“调试”时,您是指实际从 Visual Studio 的内置 Web 服务器启动应用程序进行调试,还是指附加到 IIS 中的进程?如果是前者,您应该点击 Application_Start,但如果是后者,则可能很难及早进入流程以捕捉它。
回答by Flores
I'm too having problems with breakpoints in application_start with IIS a hosted app. A good workaround is using Debugger.Break(); in code instead of the VS breakpoint
我也遇到了 application_start 与 IIS 托管应用程序中的断点问题。一个好的解决方法是使用 Debugger.Break(); 在代码中而不是 VS 断点
回答by Dao
I have just the same problem. I have made a lot of renaming in my solution. After it I got two not working web-applications and several another web-applications were all right. I got error that I have wrong routes. When I have tried to setup break point in Application_Start
method, and then restart IIS, VS didn't break execution. With workable web-applications break was working. Then I have recalled that "clean solution" and "rebuild" doesn't delete assemblies that left after renaming. And that was solution! I have manually cleaned bin
directories of my buggy-web-applications and then saw new error in Global.asax
Inherits=""
attribute was referenced old dll. I have changed it on new and break began to work. Suppose that, during renaming Global.asax wasn't updated, and IIS took old assembly (with wrong routes) to start application.
我有同样的问题。我在我的解决方案中做了很多重命名。在它之后,我得到了两个不工作的网络应用程序,而另外几个网络应用程序都正常。我有错误,我有错误的路线。当我尝试在Application_Start
方法中设置断点,然后重新启动 IIS 时,VS 没有中断执行。随着可行的网络应用程序中断工作。然后我回想起“干净的解决方案”和“重建”不会删除重命名后留下的程序集。这就是解决方案!我已经手动清理bin
了 buggy-web-applications 的目录,然后看到Global.asax
Inherits=""
属性中的新错误被引用了旧 dll。我在 new 上改变了它,break 开始工作。假设在重命名 Global.asax 期间没有更新,
回答by acezanne
Try switching the managed pipeline mode for the app pool to "Classic" instead of "Integrated". That solved the problem for me. Looking into the reason now...
尝试将应用程序池的托管管道模式切换为“经典”而不是“集成”。那为我解决了问题。现在查原因...
(Props for this answer belong to Flores (see his comment on his own answer), I just wanted to provide this as a separate answer to draw more attention to it)
(此答案的道具属于弗洛雷斯(请参阅他对自己答案的评论),我只是想将其作为单独的答案提供以引起更多关注)
回答by MemeDeveloper
Note : a nice easy alternative to using the inbuilt "Visual Studio Development Server"or IIS Express(e.g. because you are developing against IIS and have particular settingsyou need for proper functioning of your app) is to simply stay running run in IIS (I use the Custom Web Server + hosts file entry + IIS binding to same domain)
注意:使用内置的“Visual Studio 开发服务器”或IIS Express 的一个不错的简单替代方案(例如,因为您正在针对 IIS进行开发并且具有您的应用程序正常运行所需的特定设置)是简单地保持在 IIS 中运行(我使用自定义 Web 服务器 + 主机文件条目 + IIS 绑定到同一域)
- wait for debugging session to fire up ok
- then just make a whitespace edit to the root web.config and save the file
- refresh your page (Ctrl + F5)
- 等待调试会话正常启动
- 然后只需对根 web.config 进行空白编辑并保存文件
- 刷新页面 (Ctrl + F5)
Your breakpoint should be hit nicely, and you can continue to debug in your natural IIS habitat. Great !
您的断点应该很好地命中,并且您可以继续在您的自然 IIS 栖息地中进行调试。伟大的 !
回答by Caveman
Make sure that your global.asax in not under a subdirectory. It has to be placed at root level into your project.
确保您的 global.asax 不在子目录下。它必须放在项目的根级别。
回答by wonster
I had this issue in a .net 4 web forms vs2010 project and tried everything mentioned on this page. Ended up removing and adding global.asax actually resolved the issue for me.
我在 .net 4 web 表单 vs2010 项目中遇到了这个问题,并尝试了此页面上提到的所有内容。最终删除和添加 global.asax 实际上为我解决了这个问题。
回答by Glen Little
We had a similar problem, where global.asax.cs was being ignored.
我们有一个类似的问题, global.asax.cs 被忽略了。
It turns out that the site was upgraded from a precompiled .NET 2 web site to a .NET 4.0 site. On the server, the PrecompiledApp.config
file had not been deleted from the root folder. After deleting it, and recycling the IIS app pool and touching web.config to restart the application, code in Global.asax.cs started working fine.
事实证明,该站点已从预编译的 .NET 2 网站升级到 .NET 4.0 站点。在服务器上,PrecompiledApp.config
文件尚未从根文件夹中删除。删除它,并回收 IIS 应用程序池并触摸 web.config 重新启动应用程序后,Global.asax.cs 中的代码开始正常工作。