Matt
Matt

Reputation: 5778

.NET 4.5 Quartz Job not Firing Global.asax.cs Application_Start

I've been trying to get a Quartz.net job to fire inside my application and am having no luck. I wrote a test app in Visual Studio 2013 which works fine using a main method, but this is not firing in the actual application.

From what I've read, if I want something to run when the application pool is started I need a Global.asax.cs file to be placed in the App_Code folder (note that I am NOT using MVC). Then there should be a method Application_Start that will initialize the Quartz job.

So I placed the following into Global.asax.cs:

public class Global
{
    protected void Application_Start(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.Write("Hello via Debug!");
        // construct a scheduler factory
        ISchedulerFactory schedFact = new Quartz.Impl.StdSchedulerFactory();

        // get a scheduler
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        // define the job and tie it to our HelloJob class
        IJobDetail job = JobBuilder.Create<MyJob>()
            .WithIdentity("myJob", "group1")
            .Build();

        // Trigger the job to run now, and then every 40 seconds
        ITrigger trigger = TriggerBuilder.Create()
          .WithIdentity("myTrigger", "group1")
          .StartNow()
          .WithSimpleSchedule(x => x
              .WithIntervalInSeconds(5)
              .RepeatForever())
          .Build();

        sched.ScheduleJob(job, trigger);
    }
}

I have a job in another class (again, works just fine in a standard .net console app).

    public void Execute(IJobExecutionContext context)
    {
        log.Info("\n--------------------MyJob Executing via Quartz--------------------");
        log.Info("every 5 sec.");

    }

But, I'm not getting any output to the log. Would someone mind telling me what I'm doing wrong here?

Upvotes: 2

Views: 1273

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

From what I've read, if I want something to run when the application pool is started I need a Global.asax.cs file to be placed in the App_Code folder

That's incorrect.

You Add a global.asax file to your project, this has a global.asax.cs code behind. You do this through the Add new item dialog.

That's assuming this is a web application of some sort, which it's not clear from your description or tags...

Upvotes: 1

Related Questions