4

This is my code and it is not working, because apparently, IJobDetails needs to be created with job builder. Is there any way to create Job with external dependencies given in constructor?

var container = new UnityContainer();
container.RegisterType<BbProcessor, BbProcessor>();
IJobDetail jobProcessor = container.Resolve<BbProcessor>() as IJobDetail;

// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();

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

ITrigger trigger = TriggerBuilder.Create()
                    .StartNow()
                    .WithSimpleSchedule(x => x
                        .WithIntervalInSeconds(10)
                        .RepeatForever())
                    .Build();

sched.ScheduleJob(jobProcessor , trigger);

The cast is bad, but this is just example what I'm trying to do (BbProcessor is the class with dependencies given to constructor and doing work I want to do).

public class BbProcessor : IJob
{ 
    private readonly Repository _repository;
    public BbProcessor(Repository Repository) 
    { 
        _repository = Repository;
    }
}
Set
  • 47,577
  • 22
  • 132
  • 150
Piotr M
  • 499
  • 3
  • 9
  • 27

2 Answers2

1

BbProcessor implements IJob not IJobDetail which is why the cast fails. You could register the BbProcessor class with Unity using the interface instead (see here) but thats not what you really want:

var container = new UnityContainer();
container.RegisterType<IJob, BbProcessor>();
IJob jobProcessor = container.Resolve<IJob>();

In fact, BbProcessor must have a no-arg constructor. See Documentation. Also, look here for a complete example on setting up your job.

You create an IJobDetail like this:

IJobDetail job = JobBuilder.Create<BbProcessor>().WithIdentity("Job BB","Group BB").Build();

JobBuilder does not use your resolved type. See if this project where unity is integrated with quartz gets you what you're after: https://github.com/hbiarge/Quartz.Unity

Steven Magana-Zook
  • 2,751
  • 27
  • 41
  • Looks like I need to create job like this: `IJobDetail job = JobBuilder.Create() .WithIdentity("myJob", "group1") .Build();` but still, it uses empty constructr (at least looks like it) and I need to work around this – Piotr M Apr 19 '17 at 22:51
0

This post gave me answer https://stackoverflow.com/a/13857396/4350808 (I'm passing same instance to created jobs with JobDataMap)

Community
  • 1
  • 1
Piotr M
  • 499
  • 3
  • 9
  • 27