简体   繁体   English

ASP.net Core项目中的Quartz:如何获取调度器实例

[英]Quartz in ASP.net Core project: how to get the scheduler instance

I have an ASP.net Core 6 project where I added Quartz to my services, like described here:我有一个ASP.net Core 6项目,我在服务中添加了Quartz ,如下所述:

https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/aspnet-core-integration.html#installation https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/aspnet-core-integration.html#installation

This works and allows you to configure jobs and triggers during app startup.这有效并允许您在应用程序启动期间配置作业和触发器。

However, what if I want to modify/add/remove a job or trigger while the app is running?但是,如果我想在应用程序运行时修改/添加/删除作业或触发器怎么办? I would need to retrieve the underlying IScheduler instance (ideally through the DI mechanism), but I don't know the proper way to do it in an ASP.net Core app.我需要检索底层IScheduler实例(最好通过 DI 机制),但我不知道在 ASP.net 核心应用程序中执行此操作的正确方法。

So, how can you access the scheduler instance from DI in a .net core project?那么,如何在 .net 核心项目中从 DI 访问调度程序实例?

After looking at the Quartz source code, I've understood how to do this.在查看了 Quartz 源代码之后,我明白了如何做到这一点。

Indeed, you cannot inject IScheduler , in fact it wouldn't make much sense because you can have multiple schedulers configured, so the DI container would have no way to decide which one to inject.实际上,您不能注入IScheduler ,实际上这没有多大意义,因为您可以配置多个调度程序,因此 DI 容器将无法决定注入哪个调度程序。

Instead, you need to inject ISchedulerFactory , which you can then use to retrieve one of the configured schedulers.相反,您需要注入ISchedulerFactory ,然后您可以使用它来检索配置的调度程序之一。

Example code:示例代码:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddQuartz(q => {
    //configuring basic Quartz settings...
}

//Other configs...

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    var schedulerFactory = scope.ServiceProvider.GetService<ISchedulerFactory>();
    var scheduler = await schedulerFactory.GetScheduler();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET Web API Core应用程序上使用QUARTZ实现调度程序? - How to implement a scheduler using QUARTZ on an ASP.NET Web API Core application? 带有Crystal Quartz调度程序的App ASP.NET Core无法与远程调度程序一起使用 - App ASP.NET Core with Crystal Quartz scheduler doesn't work with remote scheduler 如何使用通用 asp.net 核心获取服务实例 - How to get Service instance using generic asp.net core 如何在asp.net核心中获取IConfiguration的实例? - How to get an instance of IConfiguration in asp.net core? Asp.net Core获取依赖注入实例 - Asp.net Core get instance of Dependency Injection DbContext 实例何时在 ASP.NET Core 5 中得到处置 - When does a DbContext instance get disposed in ASP.NET Core 5 ASP.NET 核心 2 + 获取数据库上下文实例 - ASP.NET Core 2 + Get instance of db context 如何在ASP.NET Core Web Application中使用Quartz.NET? - How to use Quartz.NET with ASP.NET Core Web Application? 如何在asp.net core中从多个项目中获取媒体上传公共项目的路径 - How to get Path of Common Project for Media Upload from Multiple Project in asp.net core 如何在我想要的 ASP.NET Core 2.0 中获取 DbContext 的实例? - How to get instance of DbContext in ASP.NET Core 2.0 where I want?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM