简体   繁体   English

Application_start 不起作用

[英]Application_start not working

I have written some code in the application_start() method in my global.asax file.我在global.asax文件的application_start()方法中编写了一些代码。 It does not get called when I deploy my application on IIS server.当我在IIS服务器上部署我的应用程序时,它不会被调用。 The code is accessible when I run it in the .NET framework.当我在 .NET 框架中运行代码时,它是可访问的。

I've tried to restart the application many times, but it's still not working.我已经多次尝试重新启动该应用程序,但它仍然无法正常工作。

I've also tried the suggestion from the following link.我也尝试了以下链接中的建议。

Application_Start not firing? Application_Start 没有触发?

There are few things you need to know before you are trying to debug Appplication_Start .在尝试调试Appplication_Start之前,您需要了解几件事。 There are -有 -

One : When the code executes and why it is almost impossible to debug by attaching to it.一:代码何时执行以及为什么几乎不可能通过附加到它来调试。

The application start method is executed when the application pool starts and your website is being started up for the first time.应用程序启动方法在应用程序池启动并且您的网站第一次启动时执行。 If you deploy new deliverables to IIS, then IIS might restart it itself, but there is no guarantee that it will.如果您将新的交付物部署到 IIS,则 IIS 可能会自行重新启动它,但不能保证它会重新启动。 So, deploying new codes does not guarantee that it will restart the pool and he execution of application start.因此,部署新代码并不能保证它会重新启动池并启动应用程序的执行。 You should restart your application pool to guarantee execution of application start.您应该重新启动应用程序池以保证应用程序启动的执行。

While debugging IIS applications, Visual Studio attaches itself to a process something named w3wp.exe or similart (I forgot the actual executable name), which is the worker process and only available after, remember after, your application pool is up and your site is up.在调试 IIS 应用程序时,Visual Studio 将自身附加到一个名为w3wp.exe或类似的进程(我忘记了实际的可执行文件名称),它是工作进程,只有在您的应用程序池启动并且您的站点之后才可用,记住之后向上。 So, in other words, if you are seeing this in service list, then the application start has already been executed and attaching to it will not give you a chance to debug it.因此,换句话说,如果您在服务列表中看到这个,那么应用程序启动已经被执行并且附加到它不会给您调试它的机会。 It is kind of a tug of war with time.这是一场与时间的拉锯战。

So, in other words, it is kind of impossible to debug application start unless you are very very quick.因此,换句话说,除非您非常非常快,否则调试应用程序启动是不可能的。

Two, the solution 1 - With Dev Server二、解决方案1——使用Dev Server

Launch your application in visual studio with Asp.net development server or IIS express, then you will be able to debug.在 Visual Studio 中使用 Asp.net 开发服务器或 IIS express 启动您的应用程序,然后您就可以进行调试了。 But if you really want to debug on IIS, then check the next section但是如果你真的想在 IIS 上调试,那么检查下一节

Two, the solution 2 - With IIS二、解决方案2——用IIS

There is a library in the name System.Diagnostics , Debugger and it has a nice way to call debugger in code.有一个名为System.DiagnosticsDebugger的库,它有一个很好的方式在代码中调用调试器。 You can read it here - http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx你可以在这里阅读 - http://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.break(v=vs.110).aspx

Modify you application start with this -修改您的应用程序开始于此 -

public void Application_Start(){
    ....... //other codes
    Debugger.Break() or Debugger.Launch()
}

When this line executes, IIS will halt execution, and will show you a debugger selector window (similar to the one attached)当这一行执行时,IIS 将停止执行,并会显示一个调试器选择器窗口(类似于附加的那个)在此处输入图片说明 , keep your solution open in vs and select that vs from the list, will be able to debug as usual... :) ,保持你的解决方案在 vs 中打开并从列表中选择那个 vs,将能够像往常一样调试...... :)

In case you are using windows 8 and the debugger does not launch, read this article to enable it -如果您使用的是 Windows 8 并且调试器未启动,请阅读本文以启用它 -

http://blogs.msdn.com/b/mapo/archive/2013/11/07/debugger-launch-not-displaying-jit-debugger-selection-popup-on-windows-8-8-1.aspx http://blogs.msdn.com/b/mapo/archive/2013/11/07/debugger-launch-not-displaying-jit-debugger-selection-popup-on-windows-8-8-1.aspx

Three: A very important thing三:很重要的一点

I noticed that you said, you are adding db entries in Application_Start.我注意到您说,您正在 Application_Start 中添加 db 条目。 You should keep in mind that, Application_Start does not have a HttpContext , ViewContext , So your db access code may fail for so many others reasons.您应该记住, Application_Start 没有HttpContextViewContext ,因此您的数据库访问代码可能由于许多其他原因而失败。

Make sure that the Global.asax file is actually deployed to the destination folder in the root.确保Global.asax文件实际部署到根目录中的目标文件夹。 If the file is not present then the code behind you have written for Application_Start will never be called.如果该文件不存在,则永远不会调用您为Application_Start编写的代码。

Also make sure the signature is correct还要确保签名正确

public class Global : System.Web.HttpApplication {
   protected void Application_Start(object sender, EventArgs e) {/*do something here like logging so you know it was called*/}
}

If you are running Server 2008R2 (or earlier) and/or IIS 7.5, you might want to look into the Application Initialization module.如果您运行的是 Server 2008R2(或更早版本)和/或 IIS 7.5,您可能需要查看应用程序初始化模块。 This can be downloaded here:这可以在这里下载:

www.iis.net/downloads/microsoft/application-initialization www.iis.net/downloads/microsoft/application-initialization

With IIS versions prior to 8.0, the application start is not called until the first web request arrives.对于 8.0 之前的 IIS 版本,在第一个 Web 请求到达之前不会调用应用程序启动。 I'm reading your question as you want your application start to be fired before the first web request, yes?我正在阅读您的问题,因为您希望在第一个 Web 请求之前开始触发您的应用程序,是吗?

Here is a fantastic guide to configuring this module (if it applies to you): https://blogs.msdn.microsoft.com/benjaminperkins/2014/01/07/configure-the-iis-application-initialization-module/这是配置此模块的绝佳指南(如果它适用于您): https : //blogs.msdn.microsoft.com/benjaminperkins/2014/01/07/configure-the-iis-application-initialization-module/

The key takeaways is that you need to set your app pool to 'AlwaysRunning' instead of 'OnDemand'.关键要点是您需要将应用程序池设置为“AlwaysRunning”而不是“OnDemand”。 You also need to set a preloadEnabled flag for your website.您还需要为您的网站设置一个 preloadEnabled 标志。 Once both of these are done, fire off an iisreset and you should see the results of your application start (look in the database since it's writing there).完成这两项操作后,启动 iisreset,您应该会看到应用程序启动的结果(查看数据库,因为它在那里写入)。

Other answers are relevant as well, in that this is tough to debug and you're missing all the niceties you're used to such as a httpcontext in app start.其他答案也是相关的,因为这很难调试,并且您错过了您习惯的所有细节,例如应用程序启动中的 httpcontext。

If you are running IIS 8.0 - you should still read the above link to configure preloading.如果您运行的是 IIS 8.0 - 您仍然应该阅读上面的链接来配置预加载。

This did work for me:这对我有用:

  • Menu -> Build -> Clean Solution菜单 -> 构建 -> 清洁解决方案
  • Menu -> Build -> Rebuild Solution菜单 -> 构建 -> 重建解决方案

Then, Application_Start() was fired only for the first time.然后,Application_Start() 仅第一次被触发。

在我的情况下,在生产环境中 App_global.asax.compiled 丢失,并且 global.asax 的所有内容都没有被触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM