简体   繁体   English

.net 核心石英依赖注入

[英].net Core Quartz Dependency Injection

How can I configure Quartz in .net core to use dependency injection?如何在.net内核中配置 Quartz 以使用依赖注入? I using standard .net core Dependency mechanism.我使用标准的 .net 核心依赖机制。 In constructor of class that implements IJob , I need inject some dependencies.在实现IJob的 class 的构造函数中,我需要注入一些依赖项。

How can I configure Quartz in .net core to use dependency injection?如何在.net core 中配置 Quartz 以使用依赖注入? I using standard .net core Dependency mechanism.我使用标准的 .net 核心依赖机制。 In constructor of class that implements IJob , I need inject some dependencies.在实现IJob的类的构造函数中,我需要注入一些依赖项。

How can I configure Quartz in .net core to use dependency injection?如何在.net core 中配置 Quartz 以使用依赖注入? I using standard .net core Dependency mechanism.我使用标准的 .net 核心依赖机制。 In constructor of class that implements IJob , I need inject some dependencies.在实现IJob的类的构造函数中,我需要注入一些依赖项。

How can I configure Quartz in .net core to use dependency injection?如何在.net core 中配置 Quartz 以使用依赖注入? I using standard .net core Dependency mechanism.我使用标准的 .net 核心依赖机制。 In constructor of class that implements IJob , I need inject some dependencies.在实现IJob的类的构造函数中,我需要注入一些依赖项。

How can I configure Quartz in .net core to use dependency injection?如何在.net core 中配置 Quartz 以使用依赖注入? I using standard .net core Dependency mechanism.我使用标准的 .net 核心依赖机制。 In constructor of class that implements IJob , I need inject some dependencies.在实现IJob的类的构造函数中,我需要注入一些依赖项。

UseMicrosoftDependencyInjectionJobFactory() allow to create jobs with DI UseMicrosoftDependencyInjectionJobFactory() 允许使用 DI 创建作业

        services.AddQuartz(
            q =>
            {
                q.UseMicrosoftDependencyInjectionJobFactory();
                q.ScheduleJob<JobWithDI>(trigger => {});
            }

        services.AddQuartzHostedService();

Recently I encountered a problem of resolving job's dependencies without existing scope.最近我遇到了在没有现有 scope 的情况下解决作业依赖项的问题。 I was having an exception inside job factory (implemented like in the correct answer) saying that "IServiceProvider was disposed" because the job is scheduled to be exectued after the response returns back to a client.我在作业工厂内遇到异常(如在正确答案中那样实施)说“IServiceProvider 已被处置”,因为作业计划在响应返回给客户端后执行。

So, I slightly changed implementation of IJobFactory in @CodeZombie's answer and it worked.所以,我在@CodeZombie 的回答中稍微改变了IJobFactory的实现,它起作用了。 It is different in case of using IServiceScopeFactory instead of IServiceProvider - which ensures that every time a new scope is created.在使用IServiceScopeFactory而不是IServiceProvider的情况下是不同的 - 这确保每次创建新的 scope 时。

public class CommonJobFactory : IJobFactory
    {
        private readonly ILogger<CommonJobFactory> _logger;
        private readonly IServiceScopeFactory _serviceScopeFactory;
        private readonly ConcurrentDictionary<IJob, IServiceScope> _scopes = new ConcurrentDictionary<IJob, IServiceScope>();

        public CommonJobFactory(
            IServiceScopeFactory scopeFactory, 
            ILogger<CommonJobFactory> logger)
        {
            _serviceScopeFactory = scopeFactory;
            _logger = logger;
        }

        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            var newScope = _serviceScopeFactory.CreateScope();
            try
            {
                if (newScope.ServiceProvider.GetService(bundle.JobDetail.JobType) is IJob job && _scopes.TryAdd(job, newScope))
                   return job;

                throw new NullReferenceException("Unable to create new job from scope");
            }
            catch (Exception ex)
            {
                newScope.Dispose();
                _logger.LogError(ex, "Error while constructing {jobType} from scope", bundle.JobDetail.JobType);
                throw;
            }
        }

        public void ReturnJob(IJob job)
        {
            var disposableJob = job as IDisposable;
            disposableJob?.Dispose();

            if (_scopes.TryRemove(job, out var scope))
                scope.Dispose();
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM