Skip to content

Dependency Resolution does not work for Jobs in an ASP .NET MVC application #18

@shivp

Description

@shivp

I have installed Quartz v3.2.2, Quartz.Unity v3.2.0, Unity v5.11.7 in my MVC application. I have the following lines of code in my Global.asax file in the Application_Start event .

` ///////////////////////////////////////////////

         var container = new UnityContainer();           
         container.RegisterType<ITestDependency, TestDependency>();
        container.RegisterInstance<IUnityContainer>(container, InstanceLifetime.Singleton);
        container.AddNewExtension<Quartz.Unity.QuartzUnityExtension>();
        var schedulerFactory = container.Resolve<ISchedulerFactory>();
        var scheduler = await schedulerFactory.GetScheduler(CancellationToken.None);
        container.RegisterInstance<IScheduler>(scheduler, InstanceLifetime.Singleton);
        var job = JobBuilder.Create<TestJob>()
           .WithIdentity("TestJob")
           .Build();
        var trigger = TriggerBuilder.Create()
            .WithIdentity("TestTrigger", "mygroup")
            .WithCronSchedule("0 0/2 * * * ? *") //Fire every 2 minutes
            .ForJob(job)
            .StartNow()
            .Build();
        await scheduler.ScheduleJob(job, trigger);                    
        await scheduler.Start();

`

My test job and test dependency is as follows:

`

 public class TestJob : IJob
 {
    private readonly ITestDependency testDependency;
    public TestJob(ITestDependency testDependency)
    {
        this.testDependency = testDependency;
    }

    //public TestJob()
    //{

    //}
    public Task Execute(IJobExecutionContext context)
    {
        var task = Task.Run(() => {
            testDependency.DoWork();                
        });
        return task;
    }
}

public interface ITestDependency
{
    void DoWork();
}
public class TestDependency : ITestDependency
{
    public void DoWork()
    {
        Thread.Sleep(10000);
    }
}
`

The TestJob Job never fires. If I comment the TestJob constructor with parameter and uncomment the parameterless constructor, then the TestJob gets called. I tried out different things such as moving out the code to Startup.cs in the Configuration method but the issue remains.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions