2

I'm trying to do some scheduled jobs with Quartz.NET. When I use constructor in TestJob class for injection, break point not comes here. When I use property injection in AutofacSettings, it can not set the value of property _userService.

I've tried lots of methods, read so many articles but I'm stuck on this problem.

This solution looks like an answer but due to version updates (I think), it doesn't works for me: How do I create a Quartz.NET’s job requiring injection with autofac I

AutofacSettings

public class AutofacSettings
{
    public static void Run()
    {
        ContainerBuilder builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerRequest();
        builder.RegisterType<DbFactory>().As<IDbFactory>().InstancePerRequest();
        builder.RegisterAssemblyTypes(Assembly.Load("DepremsizHayat.Business")).Where(p => p.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerRequest();
        builder.RegisterAssemblyTypes(Assembly.Load("DepremsizHayat.Business")).Where(p => p.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerRequest();
        RegisterScheduler(builder);
        IContainer container = builder.Build();
        ConfigureScheduler(container);
        AutofacDependencyResolver resolver = new AutofacDependencyResolver(container);
        DependencyResolver.SetResolver(resolver);
    }

    private static void RegisterScheduler(ContainerBuilder builder)
    {
        var schedulerConfig = new NameValueCollection {
          {"quartz.threadPool.threadCount", "3"},

         };
        builder.RegisterModule(new QuartzAutofacFactoryModule
        {
            ConfigurationProvider = c => schedulerConfig
        });
        builder.RegisterModule(new QuartzAutofacJobsModule(typeof(TestJob).Assembly));
        builder.RegisterType<TestJobScheduler>().AsSelf();
    }
    private static void ConfigureScheduler(IContainer container)
    {
        var scheduler = container.Resolve<TestJobScheduler>();
        scheduler.Start();
    }
}

TestJob

public class TestJob : IJob
{
    public IUserService _userService { get; set; }
    Task IJob.Execute(IJobExecutionContext context)
    {
        System.Diagnostics.Debug.WriteLine(_userService.GetById(1).FIRST_NAME.ToString());
        return Task.CompletedTask;
    }
}

TestJobScheduler

public class TestJobScheduler
{
    private readonly IScheduler _scheduler;
    public TestJobScheduler(IScheduler scheduler)
    {
        this._scheduler = scheduler;
    }
    public void Start()
    {
        _scheduler.Start();
        IJobDetail job = JobBuilder.Create<TestJob>().Build();
        ITrigger trigger = TriggerBuilder.Create().WithSimpleSchedule(
            s =>
            s
            .WithIntervalInSeconds(30).WithRepeatCount(3)
            ).StartNow().Build();
        _scheduler.ScheduleJob(job, trigger);
    }
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • You mention a constructor in `TestJob` but your code example does not have that. Also, which location would your breakpoint be at? – Peter Lillevold Jan 24 '21 at 20:44
  • I deleted it, because IJob doesn’t want a constructor with parameter as I said. Break points are in constructor and Execute method. Without ctor, break point comes in there. – Uzay Yiğit GÖK Jan 24 '21 at 20:49
  • 1
    I see no reason why `TestJob` couldn't have a ctor. Accoring to the `QuartzAutofacJobsModule` (https://github.com/alphacloud/Autofac.Extras.Quartz/blob/83d9d95f1a138ddb73787213892d3580b8bb0756/src/Autofac.Extras.Quartz/QuartzAutofacJobsModule.cs) your job would be registered as "self" and "instance per lifetime scope". So as long as your container contains the services requested by TestJob ctor, that will work fine. Note that by default, the `QuartzAutofacJobsModule` will not register jobs with property injetion unless you set `AutoWireProperties` to true – Peter Lillevold Jan 24 '21 at 21:00
  • 1
    When I'm having trouble getting what I expect from my container setup, I always put up some unit tests that tries to resolve various things from the container. That way I can more easily target which services are incorrectly registered, or which services are not registered at all. – Peter Lillevold Jan 24 '21 at 21:02

0 Answers0