Autofac.Extras.Quartz示例代码崩溃

时间:2017-08-23 03:50:52

标签: autofac quartz.net

我尝试使用Quartz和Autofac Autofac.Extras.Quartz库。但是我的代码崩溃了 它很简单但崩溃了:

sqrt()

,例外:

using Autofac;
using Autofac.Extras.Quartz;
using Quartz;
using System;

namespace Test1_netCore
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterModule(new QuartzAutofacFactoryModule());
            var container = builder.Build();
            IScheduler scheduler = container.Resolve<IScheduler>(); //crashes here
            //
        }
    }
}

然而,当我解决Autofac.Core.DependencyResolutionException: 'An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = IScheduler (DelegateActivator), Services = [Quartz.IScheduler], Lifetime = Autofac.Core.Lifetime.RootScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope' Inner Exception MissingMethodException: Method not found: "Quartz.IScheduler Quartz.ISchedulerFactory.GetScheduler()". 并从中获取容器

时,这运行正常
ISchedulerFactory

会发生什么?我想我强烈误解了一些事情。

1 个答案:

答案 0 :(得分:1)

我弄明白:Autofac.Extras.Quartz和Quartz程序集不兼容。 Autofac.Extras.Quartz(3.3.0)调用ISchedulerFactory方法getScheduler()就像这样

builder.Register(c => c.Resolve<ISchedulerFactory>().GetScheduler())

https://github.com/alphacloud/Autofac.Extras.Quartz/blob/26f33fa3be91a077e9edfefbc53606ab2f2ffc95/src/Autofac.Extras.Quartz/QuartzAutofacFactoryModule.cs#L89

但是在Quarts 3.0.0-alpha3中这个方法的签名是

Task<IScheduler> GetScheduler(CancellationToken cancellationToken = default(CancellationToken));

https://github.com/quartznet/quartznet/blob/bb682fe4c051b2120086170cf03ae5111832b29f/src/Quartz/ISchedulerFactory.cs#L47

但是,在我的问题中,我可以在第二个代码示例中使用版本和方法,只需手动解析ISchedulerFactory然后getSchduler()。 (我真的需要这个版本的包装,因为我项目中的其他依赖项)