Внедрение зависимостей в Quartz.net Core не находит зарегистрированных сервисов - PullRequest
1 голос
/ 17 июня 2019

У меня возникает проблема с регистрацией DI при попытке настройки Quartz. Это простое тестовое задание для подтверждения того, что DI работает (просто выводит текст на консоль).

Код ошибкиброшен в последнем классе JobFactory.

Program.cs

static async Task Main(string[] args)
{

    var isService = !(Debugger.IsAttached || ((IList)args).Contains("--console"));

    var path = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);

    var webHost = new HostBuilder()
        .ConfigureAppConfiguration((cxt, config) =>
        {
            config.SetBasePath(path);
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            config.AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();

            if (args != null)
            {
                config.AddCommandLine(args);
            }

            Log.Logger = new LoggerConfiguration()
                    .ReadFrom.Configuration(config.Build())
                    .Enrich.FromLogContext()
                    .CreateLogger();
        })
        .ConfigureServices((cxt, services) =>
        {
            var configuration = cxt.Configuration;

            var bw = new BackgroundWorker(services.BuildServiceProvider());
            services.AddSingleton<IHostedService>(bw);
            services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));

            //services.AddSingleton<ISchedulerFactory, SchedulerFactory>();
            //services.AddSingleton<ScopedJobFactory>();

            services.AddScoped(_ => new SomeJob(configuration));
            //services.AddTransient<IJob>(_ => new SomeJob(configuration));

    var token = tokenSource.Token;
    if (isService)
    {
        await webHost.RunAsServiceAsync(token);
    }
    else
    {
        await webHost.RunConsoleAsync(token);
    }
}

Настройка фабрики заданий Quartz:

private static async Task<IScheduler> InitiateQuartzScheduler(IServiceProvider serviceProvider)
{
    try
    {
        var factory = new StdSchedulerFactory();
        var scheduler = await factory.GetScheduler();
        scheduler.JobFactory = new JobFactory(serviceProvider);

        await scheduler.Start();

        return scheduler;

    }
    catch (SchedulerException se)
    {
        Log.Logger.Fatal(se, "Error at starting the Quartz Scheduler");
    }

    return null;
}

Фоновый рабочий:

private class BackgroundWorker : IHostedService
{
    private IScheduler quartzScheduler;
    private readonly IServiceProvider serviceProvider;

    public BackgroundWorker(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        //Log.Logger = SetupSerilog();
        Log.Logger.Information("Starting Quartz BackgroundWorker.");

        quartzScheduler = await InitiateQuartzScheduler(serviceProvider);
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        Log.Logger.Information("Quartz Background Worker is stopping.");
    }
}

Фабрика заданий (где происходит ошибка):

internal class JobFactory : IJobFactory
{
    protected readonly IServiceProvider serviceProvider;

    protected readonly ConcurrentDictionary<IJob, IServiceScope> _scopes = new ConcurrentDictionary<IJob, IServiceScope>();

    public JobFactory(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        var scope = serviceProvider.CreateScope();
        IJob job;

        try
        {
            //
            // **ERROR HERE**
            //
            job = scope.ServiceProvider.GetRequiredService(bundle.JobDetail.JobType) as IJob;

        }
        catch
        {
            // Failed to create the job -> ensure scope gets disposed
            scope.Dispose();
            throw;
        }

        // Add scope to dictionary so we can dispose it once the job finishes
        if (!_scopes.TryAdd(job, scope))
        {
            // Failed to track DI scope -> ensure scope gets disposed
            scope.Dispose();
            throw new Exception("Failed to track DI scope");
        }

        return job;
    }

    public void ReturnJob(IJob job)
    {
        if (_scopes.TryRemove(job, out var scope))
        {
            // The Dispose() method ends the scope lifetime.
            // Once Dispose is called, any scoped services that have been resolved from ServiceProvider will be disposed.
            scope.Dispose();
        }
    }
}

Ошибка времени выполнения:

System.InvalidOperationException: «Служба для типа« xxx.yyy.SomeJob »не зарегистрирована».

1 Ответ

1 голос
/ 17 июня 2019

Фоновому работнику предоставляется поставщик до добавления всех необходимых зависимостей.

//...

var configuration = cxt.Configuration;

var bw = new BackgroundWorker(services.BuildServiceProvider()); //<---This service provider
services.AddSingleton<IHostedService>(bw);
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));

services.AddScoped(_ => new SomeJob(configuration)); //<--knows nothing about this service

//...or any other service added after services.BuildServiceProvider()

//...

После создания коллекции служб любые изменения (добавления / удаления) из коллекции не влияют на уже созданного поставщика.

Рассмотрите возможность изменения подхода и использования фабрики отложенных делегатов.при регистрации работника

//...

services.AddSingleton<IHostedService>(serviceProvider => new BackgroundWorker(serviceProvider));

//...
...