【问题标题】:Application_Start Event in Global.asax not FiringGlobal.asax 中的 Application_Start 事件未触发
【发布时间】:2015-09-22 18:16:35
【问题描述】:

我创建了一个每分钟运行一次的任务,并决定将它放在Global.asax 文件中的Application_Start 事件中,以便在应用程序启动后立即执行。当我运行它(没有调试并处于调试模式)时,它不会启动。

我试图把它放在Session_Start 事件中只是为了确保它不是来自Global.asax 文件的标记并且它工作得很好。但不幸的是,它必须是在应用程序启动时,而不是在会话启动时。

这是我的 Global.asax (ASP.NET 2.0, VS2008) 下面:

<%@ Application Language="C#" Inherits="Microsoft.Practices.CompositeWeb.WebClientApplication" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="TaskManager" %>

<script runat="server">

    private TaskScheduler _scheduler = null;

    void Application_Start(object sender, EventArgs e) 
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(Server.MapPath(@"\Config\tasks.config"));
        XmlNodeList nodes = xml.SelectNodes("Tasks/Task");

        this._scheduler = new TaskScheduler(nodes);
        this._scheduler.StartTasks();
    }

    void Application_End(object sender, EventArgs e) 
    {
        this._scheduler.StopTasks();
    }

    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

</script>

【问题讨论】:

  • 在回收应用程序池/IIS(无论您在哪里托管应用程序)后尝试
  • @VimalanJayaGanesh 感谢您的回复。它托管在 ASP.NET 开发服务器上。我该如何回收它?
  • 所以,基本上 Application_Start 事件只在应用程序第一次运行时运行一次。它会在应用程序重新启动时再次运行(例如:webconfig 中的更改、应用程序池回收或 IIS 重新启动等)。如果您想快速验证,只需尝试在您的 web.config 中进行少量编辑并再次运行该应用程序。这一次,断点应该命中 Application_Start 方法。
  • @VimalanJayaGanesh 我编辑了 web.config,清理并重建了解决方案,但没有任何乐趣。没有命中断点。

标签: c# asp.net


【解决方案1】:

抱歉 - 由于代表我无法发表评论 - 但如果遇到问题,您可以求助于 System.Diagnostics.Debugger.Launch();结合上述任何一项(编辑 web.cofig 等)

【讨论】: