Quartz.Net Dependency Injection .Net Core

前端 未结 4 881
无人及你
无人及你 2021-01-11 15:08

In my project I have to use Quartz but I don\'t know what i do wrong.

JobFactory:

public class IoCJobFactory :         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-11 15:43

    This is how I did it in my application. Instead of adding the Scheduler to the ioc I only add the factory

    services.AddTransient(
                    (provider) =>
                    {
                        return new AspJobFactory( provider );
                    } );
    

    My job factory pretty much looks the same. Transient does not really matter as I only use this once anyway. My use Quartz extension method then is

    public static void UseQuartz(this IApplicationBuilder app, Action configuration)
            {
                // Job Factory through IOC container
                var jobFactory = (IJobFactory)app.ApplicationServices.GetService( typeof( IJobFactory ) );
                // Set job factory
                Quartz.Instance.UseJobFactory( jobFactory );
    
                // Run configuration
                configuration.Invoke( Quartz.Instance );
                // Run Quartz
                Quartz.Start();
            }
    

    The Quartz class is Singleton as well.

提交回复
热议问题