Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shivp opened this issue Oct 30, 2020 · 0 comments
Open

Comments

@shivp
Copy link

shivp commented Oct 30, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant