.net Core Quartz Dependency Injection

后端 未结 4 2037
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 07:34

How can I configure Quartz in .net core to use dependency injection? I using standard .net core Dependency mechanism. In constructor of class that implemen

4条回答
  •  无人及你
    2020-12-03 08:10

    No idea if this will be helpful or not but I created my own DI extension for Quartz that you are more then welcome to try: https://github.com/JaronrH/Quartz.DependencyInjection

    Short version is that you would use the AddQuartz() method to pass in the [optional] NaveValueCollection config and [required] Scrutor assembly searching you want (see https://andrewlock.net/using-scrutor-to-automatically-register-your-services-with-the-asp-net-core-di-container/). For example:

    services.AddQuartz(s => s.FromAssemblyOf())
    

    This call will:

    • Find and automatically register all IJob, IAddScheduledJob, IAddSchedulerListener, IAddTriggerListener, and IAddJobListener implementations found in the assemblies (Scrutor). So, yes, you can use DI in your IJob classes this way!
    • Setup a Singleton IScheduler in DI that uses the above DI resources.
    • Register an adapter to the IScheduler so that Microsoft's Logging is used for Quartz's logging.

    You can then either use provider.StartQuartz() to start the Scheduler (which automatically looks for IApplicationLifetime and registers the Scheduler for Shutdown if available) or use conventional DI to get and start the services (provider.GetService().Start();).

    Hope this helps!

提交回复
热议问题