Quartz.Net 具有多级 DI 的作业

Quartz.Net Job with multiple levels of DI

我有一个 JobFactory : IJobFactory 可以用。我可以很好地创建预定作业,并且可以毫无问题地注入简单的依赖项,但我确实有一个问题。依赖项之一 本身 依赖于 HttpClient,但这似乎不起作用。

例如

services.AddHttpClient<Dependency>("dependency");
services.AddSingleton(typeof(Dependency));

using (var serviceProvider = services.BuildServiceProvider())
{
  var schedulerFactory = new StdSchedulerFactory();
  var scheduler = await schedulerFactory.GetScheduler();
  scheduler.JobFactory = new JobFactory(serviceProvider);
  await scheduler.Start();

  var job = JobBuilder.Create<Job1>()
                .WithIdentity("job1")
                .Build();
  var trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(1)
                    .RepeatForever())
                .Build();
  await scheduler.ScheduleJob(job, trigger);
}

依赖构造函数是

public Dependency(HttpClient httpClient)
{
  // <snipped>
}

并且作业构造函数是

public Job1(Dependency dependency)
{
  // <snipped>
}

当我尝试 运行 时,我被告知作业抛出未处理的异常。通过我的 JobFactory 调试告诉我 httpClient 根本没有被注入。 .AddHttpClient 不应该处理这个吗?是不是因为DI的层级多了才行不通?有没有可能换一种方式?

请注意,我也尝试像 Dependency 一样手动注册 Job1,但这并没有解决问题。

这段代码重构的时机已经成熟。

将调度程序放入 IHostedService 并让它处理调度程序的启动。

public interface IHostedService {
    //
    // Summary:
    //     Triggered when the application host is ready to start the service.
    Task StartAsync(CancellationToken cancellationToken);
    //
    // Summary:
    //     Triggered when the application host is performing a graceful shutdown.
    Task StopAsync(CancellationToken cancellationToken);
}

public class SchedulerService : IHostedService {
    readonly IJobFactory jobFactory;
    readonly ISchedulerFactory schedulerFactory
    IScheduler  scheduler;

    public SchedulerService(IJobFactory jobFactory, ISchedulerFactory schedulerFactory) {
        this.jobFactory = jobFactory;
        this.schedulerFactory = schedulerFactory;
    }

    public async Task StartAsync(CancellationToken cancellationToken) {
        scheduler = await schedulerFactory.GetScheduler();
        scheduler.JobFactory = jobFactory;

        IJobDetail job = JobBuilder.Create<Job1>()
            .WithIdentity("job1")
            .Build();

        ITrigger  trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1")
            .StartNow()
            .WithSimpleSchedule(x => x.WithIntervalInSeconds(1)
                .RepeatForever())
            .Build();

        await scheduler.ScheduleJob(job, trigger);

        await scheduler.Start(cancellationToken);
    }

    public Task StopAsync(CancellationToken cancellationToken) {
        return scheduler.Shutdown(cancellationToken);
    }
}

除此之外,现在可以通过将所有类型添加到服务集合来在启动时干净地完成配置

class Program {
    static async Task Main(string[] args) {

        var services = new ServiceCollection();

        //...

        services.AddHttpClient<IDependency, Dependency>();
        services.AddScoped<Job1>();
        services.AddTransient<ISchedulerFactory, StdSchedulerFactory>();
        services.AddTransient<IJobFactory>(serviceProvider => new JobFactory(serviceProvider)); 
        services.AddTransient<IHostedService, SchedulerService>();

        //...

        IServiceProvider serviceProvider = services.BuildServiceProvider();

        var service = serviceProvider.GetRequiredService<IHostedService>(); 
        await service.StartAsync();

        Console.ReadKey();

    }
}

现在服务将管理启动,所有必要的依赖项也将根据需要注入。

包括您的类型化客户端 Dependency class 假设如下

public class Dependency : IDependency {

    public Dependency(HttpClient httpClient) {
      // <snipped>
    }
}

public class Job1: IJob {
    public Job1(IDependency dependency) {
      // <snipped>
    }
}

事实证明,我同时遇到了三个独立且不相关的问题:

  1. AddHttpClient<Dependency>() 注册我的依赖项,我不应该也包含 AddSingleton<Dependency>().
  2. 我需要自己注册工作:services.AddScoped<Job1>();(谢谢@Nkosi)
  3. 我的工作有一个 internal 构造函数。它需要 public。 (可能是因为 static async Task Main(string[] args) 是静态的,但我不确定。)