如何将 Quartz.NET 与 ASP.NET 核心 Web 应用程序一起使用?

How to use Quartz.NET with ASP.NET Core Web Application?

在传统的 ASP.NET 应用程序中,我们在 global.asax.csApplication_Start 处理程序中(重新)初始化 Quartz.NET 调度程序。 但是我不知道在哪里编写调度作业的代码,因为 ASP.NET 核心 Web 应用程序 中没有 global.asax.cs。 我应该把代码放在 Startup.cs 吗?

您可以使用 ConfigureServicesConfigure 方法。 虽然 Configure 方法主要用于配置 HTTP 请求管道,但好处是您可以直接使用 IHostingEnvironment (从而获取配置设置)和 ILoggerFactory 接口。如果您在 Startup class.

中创建相应的属性,则使用 ConfigureServices 方法可以访问这些依赖项
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

在 Startup.cs 文件中,这相当于 asp.net 核心。

您甚至可以为 IServiceCollection 创建一个扩展方法 class 以使代码简洁,因此代码应该如下所示

public void ConfigureServices(IServiceCollection services)
{
  services.AddQuartz(new QuartezOptions {});
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  app.UseQuartz();
}