0

I created a task to run every minute and decided to put it in the Application_Start event in Global.asax file to execute as soon as the application is started. When I run it (without debugging and in debug mode), It doesn't fire up.

I tried to put it in Session_Start event just to make sure it's not from the markup of the Global.asax file and it worked perfectly. But unfortunately, It has to be when the Application starts and not when a session starts.

This is my Global.asax (ASP.NET 2.0, VS2008) below:

<%@ 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>
halfer
  • 19,824
  • 17
  • 99
  • 186
Kacey Ezerioha
  • 1,068
  • 4
  • 22
  • 46
  • Try after recycling the app pool / IIS (wherever you have hosted your application) – JGV Sep 22 '15 at 18:23
  • @VimalanJayaGanesh Thanks for your response. It's hosted on ASP.NET Development server. How do I recycle that? – Kacey Ezerioha Sep 22 '15 at 18:25
  • 1
    So, basically Application_Start event runs only once when the application first time runs. It run again whenever the application restarts (example: change in webconfig, application pool recycle or IIS restarted etc). If you want to quickly verify, just try to make a minor edit in your web.config and run the application again. This time, the breakpoint should hit the Application_Start method. – JGV Sep 22 '15 at 18:32
  • @VimalanJayaGanesh I edited the web.config, cleaned and rebuilt the solution but no joy. The breakpoint didn't get hit. – Kacey Ezerioha Sep 22 '15 at 18:40
  • These might help: http://stackoverflow.com/questions/641148/application-start-not-firing and http://stackoverflow.com/questions/693020/application-start-not-being-hit-in-asp-net-web-app – JGV Sep 22 '15 at 18:47
  • It's very challenging to step into Global.asax via Visual Studio. The easiest way I've found is to host the site in IIS Express for debugging/development purposes (though I strongly prefer Local IIS and only use express when I need to see what's happening in Global.asax). – Tim Sep 22 '15 at 18:58
  • I restarted IIS but no joy. **sad face** – Kacey Ezerioha Sep 22 '15 at 19:21

1 Answers1

0

Sorry - I cant comment due to rep - but if having trouble you could resort to System.Diagnostics.Debugger.Launch(); in combination with any of the above (editing web.cofig etc)

kenam
  • 125
  • 8