Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Quartz.Net向DI注册作业_C#_Asp.net Mvc 4_Dependency Injection_Quartz.net - Fatal编程技术网

C# Quartz.Net向DI注册作业

C# Quartz.Net向DI注册作业,c#,asp.net-mvc-4,dependency-injection,quartz.net,C#,Asp.net Mvc 4,Dependency Injection,Quartz.net,我正在使用Quartz.Net windows服务在带有Castle windsor依赖项注入的MVC4应用程序上运行远程作业。我想安排在执行时引用模型接口的作业来访问模型函数 这是我的职业课: public class MyJob: IJob { private static readonly ILog logger = LogManager.GetLogger(typeof(MyJob)); private readonly IQuartzM

我正在使用Quartz.Net windows服务在带有Castle windsor依赖项注入的MVC4应用程序上运行远程作业。我想安排在执行时引用模型接口的作业来访问模型函数

这是我的职业课:

    public class MyJob: IJob 
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(MyJob));
        private readonly IQuartzModel _quartzModel;

        public MyJob(IQuartzModel quartzModel)
        {
           this._quartzModel = quartzModel;
        }

       public void Execute(IJobExecutionContext context)
       {
            quartzModel.DoModelFunction();       
       }
  }
我创建自己的IJobFactory实例,如下所示:

public class WindsorJobFactory : IJobFactory
{
    private readonly IWindsorContainer _container;

    public WindsorJobFactory(IWindsorContainer container)
    {
        _container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle)
    {
        if (bundle == null)
        {
            throw new ArgumentNullException("bundle");
        }
        return (IJob)_container.Resolve(bundle.JobDetail.JobType);
    }
 }
我已经在castle DI创建了这些注册

        IJobFactory jobFactory = new WindsorJobFactory(container);

        container.Register(Component.For<IJobFactory>().Instance(jobFactory));
        container.Register(Component.For<IQuartzModel>().ImplementedBy<QuartzModel>());

        var jobTypes = GetJobTypes();
        foreach (Type jobType in jobTypes)
        {
              container.Register(Component.For(jobType).ImplementedBy(jobType).LifeStyle.Transient);
        }

    }

    private static IEnumerable<Type> GetJobTypes()
    {
        return AppDomain.CurrentDomain.GetAssemblies().ToList()
            .SelectMany(s => s.GetTypes())
            .Where(p => typeof(IJob).IsAssignableFrom(p) && !p.IsInterface);
    }
如果我执行MyJob时没有在构造函数中传递接口,它将成功执行

要运行作业,我从控制器初始化QuartzTaskSchedulingService并执行作业创建方法。在QuartzTaskSchedulingService中,我对IsSchedulerFactory、IsScheduler进行了初始化。在构造函数中,我使用IQuartzSchedulingServiceSettings提供的连接设置来获取调度程序的实例

    public QuartzTaskSchedulingService(IQuartzSchedulingServiceSettings settings)
    {
        this.settings = settings;

        Address = string.Format("tcp://{0}:{1}/{2}", settings.ServerAddress, settings.ServerPort, settings.SchedulerBindName);
        schedulerFactory = new StdSchedulerFactory(GetProperties(Address, settings.ServerInstanceName));

            try
            {
                quartzScheduler = schedulerFactory.GetScheduler();
                if (!quartzScheduler.IsStarted)
                {
                    quartzScheduler.Start();
                }
            }
      }
之后,它跳转到创造就业机会的方法

    public void TestJobShot(Type t)
    {

        IJobDetail job = JobBuilder.Create(t)
            .WithIdentity("MyJob", "Common")
            .RequestRecovery()
            .Build();

        var trigger = (ICronTrigger)TriggerBuilder.Create()
            .WithIdentity("MyJob", "Common")
            .WithCronSchedule("0 0/1 * 1/1 * ? *")
            .StartAt(DateTime.UtcNow)
            .WithPriority(1)
            .Build();

        quartzScheduler.ScheduleJob(job, trigger);

    }
它成功地创建了作业,并计划从我的AdoJobStore执行。一旦它执行,我就会遇到以下问题

我已经尝试了很多解决方案,但都没有成功 我是错过了某种DI注册还是做错了什么


谢谢

如果我没有弄错的话,您的容器没有正确解析IQuartzModel。
我也有同样的问题,但使用Autofac。

你能演示一下如何运行作业吗?Cybermaxs-Betclic,我编辑了这篇文章来回答你的问题。谢谢
    public QuartzTaskSchedulingService(IQuartzSchedulingServiceSettings settings)
    {
        this.settings = settings;

        Address = string.Format("tcp://{0}:{1}/{2}", settings.ServerAddress, settings.ServerPort, settings.SchedulerBindName);
        schedulerFactory = new StdSchedulerFactory(GetProperties(Address, settings.ServerInstanceName));

            try
            {
                quartzScheduler = schedulerFactory.GetScheduler();
                if (!quartzScheduler.IsStarted)
                {
                    quartzScheduler.Start();
                }
            }
      }
    public void TestJobShot(Type t)
    {

        IJobDetail job = JobBuilder.Create(t)
            .WithIdentity("MyJob", "Common")
            .RequestRecovery()
            .Build();

        var trigger = (ICronTrigger)TriggerBuilder.Create()
            .WithIdentity("MyJob", "Common")
            .WithCronSchedule("0 0/1 * 1/1 * ? *")
            .StartAt(DateTime.UtcNow)
            .WithPriority(1)
            .Build();

        quartzScheduler.ScheduleJob(job, trigger);

    }