我试图让Quartz.net(2.1.2)使用IoC容器(autofac),因为我需要在计划的作业中使用服务.我已经找到了关于这个主题的类似帖子,但是我似乎找不到具有自动填充的特定注册示例.
以下帖子涉及我遇到的相同问题:
How to schedule task using Quartz.net 2.0?
但是,我相信我失踪的部分是当答案说“不要忘记在IoC容器中注册这个工作”.我不确定如何做到这一点,因为迄今为止所尝试的一切都没有奏效.
在下面的示例中,“HelloJob”将会运行,但是当我尝试将releaseService注入到“ReleaseJob”中时,它将拒绝运行.
更新:
我在DependencyRegistration.cs部分中标记了我认为问题的代码.
更新2:
一些与我需要做的和可能有帮助的相关链接(我已经通过它们,但仍然无法弄清楚如何使用autofac)
如何在PRO中使用Quartz.NET?
– http://blog.goyello.com/2009/09/21/how-to-use-quartz-net-in-pro-way/
Autofac和Quartz.NET
– http://blog.humann.info/post/2013/01/30/Autofac-and-QuartzNET.aspx
Quartz.NET和简单注射器的构造器注入
– Constructor injection with Quartz.NET and Simple Injector
ASP.Net MVC 3,Ninject和Quartz.Net – 如何?
– ASP.Net MVC 3,Ninject and Quartz.Net – How to?
以下是相关代码:
Global.asax中
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); var dependencyRegistration = new DependencyRegistration(); dependencyRegistration.Register(); ModelValidatorProviders.Providers.Clear(); ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new ValidatorFactory())); DataAnnotationsModelValidatorProvider.AddImplicitrequiredAttributeForValueTypes = false; }
DependencyRegistration.cs
public class DependencyRegistration { public void Register() { var builder = new ContainerBuilder(); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly()); // Validation builder.RegisterType<ValidatorFactory>() .As<IValidatorFactory>() .InstancePerHttpRequest(); AssemblyScanner findValidatorsInAssembly = AssemblyScanner.FindValidatorsInAssembly(Assembly.GetExecutingAssembly()); foreach (AssemblyScanner.AssemblyScanResult item in findValidatorsInAssembly) { builder.RegisterType(item.ValidatorType) .As(item.InterfaceType) .InstancePerHttpRequest(); } // Schedule builder.Register(x => new StdSchedulerFactory().GetScheduler()).As<IScheduler>(); // Schedule jobs builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(x => typeof(IJob).IsAssignableFrom(x)); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //Schedule IScheduler sched = container.Resolve<IScheduler>(); sched.JobFactory = new AutofacJobFactory(container); sched.Start(); IJobDetail job = JobBuilder.Create<ReleaseJob>() .WithIdentity("1Job") .Build(); ITrigger trigger = TriggerBuilder.Create() .WithIdentity("1JobTrigger") .WithSimpleSchedule(x => x .RepeatForever() .WithIntervalInSeconds(5) ) .StartNow() .Build(); sched.ScheduleJob(job,trigger); job = JobBuilder.Create<HelloJob>() .WithIdentity("2Job") .Build(); trigger = TriggerBuilder.Create() .WithIdentity("2JobTrigger") .WithSimpleSchedule(x => x .RepeatForever() .WithIntervalInSeconds(5) ) .StartNow() .Build(); sched.ScheduleJob(job,trigger); } }
JobFactory.cs
public class AutofacJobFactory : IJobFactory { private readonly IContainer _container; public AutofacJobFactory(IContainer container) { _container = container; } public IJob NewJob(TriggerFiredBundle bundle,IScheduler scheduler) { return (IJob)_container.Resolve(bundle.JobDetail.JobType); } public void ReturnJob(IJob job) { } }
ReleaseJob.cs
public class ReleaseJob : IJob { private readonly IReleaseService _releaseService; public ReleaseJob(IReleaseService releaseService) { this._releaseService = releaseService; } public void Execute(IJobExecutionContext context) { Debug.WriteLine("Release running at " + DateTime.Now.ToString()); } } public class HelloJob : IJob { public void Execute(IJobExecutionContext context) { Debug.WriteLine("Hello job at " + DateTime.Now.ToString()); } }
ReleaseServiceModel.cs
public class ReleaseServiceModule : Module { protected override void Load(ContainerBuilder builder) { builder.RegisterType<ReleaseService>() .As<IReleaseService>() .InstancePerLifetimeScope(); } }