Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

Quartz.NET 3.2.3 with Autofac 6.1.0 Dependency Injection ASP.Net MVC

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);
    }
}
question from:https://stackoverflow.com/questions/65874890/quartz-net-3-2-3-with-autofac-6-1-0-dependency-injection-asp-net-mvc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...