使用Topshelf和Quartz.NET

时间:2017-07-04 07:23:30

标签: c# topshelf quartz

我在设置基本的Quartz.NET和Topshelf集成时遇到了问题。

访问ScheduleQuartzJob时出现错误:

Error   1   Delegate 'System.Func<ServiceTest.MyService>' does not take 1 arguments 
Error   2   Not all code paths return a value in lambda expression of type 'System.Func<Topshelf.Runtime.HostSettings,ServiceTest.MyService>'
Error   3   'Topshelf.Runtime.HostSettings' does not contain a definition for 'ConstructUsing' and the best extension method overload 'Topshelf.ServiceConfiguratorExtensions.ConstructUsing<T>(Topshelf.ServiceConfigurators.ServiceConfigurator<T>, System.Func<T>)' has some invalid arguments
Error   4   Instance argument: cannot convert from 'Topshelf.Runtime.HostSettings' to 'Topshelf.ServiceConfigurators.ServiceConfigurator<ServiceTest.MyService>'
Error   5   'Topshelf.Runtime.HostSettings' does not contain a definition for 'WhenStarted' and no extension method 'WhenStarted' accepting a first argument of type 'Topshelf.Runtime.HostSettings' could be found (are you missing a using directive or an assembly reference?)
Error   6   'Topshelf.Runtime.HostSettings' does not contain a definition for 'WhenStopped' and no extension method 'WhenStopped' accepting a first argument of type 'Topshelf.Runtime.HostSettings' could be found (are you missing a using directive or an assembly reference?)
Error   7   'Topshelf.ServiceConfigurators.ServiceConfigurator<ServiceTest.MyService>' does not contain a definition for 'ScheduleQuartzJob' and no extension method 'ScheduleQuartzJob' accepting a first argument of type 'Topshelf.ServiceConfigurators.ServiceConfigurator<ServiceTest.MyService>' could be found (are you missing a using directive or an assembly reference?)

我想我错过了一些明显的东西......

附带问题:是否有更简单的方法来设置Quartz / Topshelf配置?

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using NLog;
using Quartz;
using Quartz.Impl;
using Common.Logging.Configuration;
using Common.Logging.NLog;
using Topshelf;


namespace ServiceTest
{
    public class MyService
    {
        public bool Start(HostControl control)
        {
            return true;
        }

        public bool Stop(HostControl control)
        {
            return true;
        }
    }

    public class Program
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        
        public static void Main(string[] args)
        {
            var config = new NameValueCollection();
            var adaptor = new NLogLoggerFactoryAdapter(config);
            Common.Logging.LogManager.Adapter = adaptor;

            HostFactory.Run(x =>
            {
                x.Service<MyService>(sc =>
                {
                    sc.ConstructUsing(() => new MyService());
                    sc.WhenStarted((service, control) => service.Start(control));
                    sc.WhenStopped((service, control) => service.Stop(control));

                    sc.ScheduleQuartzJob(q =>
                        q.WithJob(() =>
                            JobBuilder.Create<MyJob>().Build())
                            .AddTrigger(() => TriggerBuilder.Create()
                                .WithSimpleSchedule(b => b
                                    .WithIntervalInSeconds(10)
                                    .RepeatForever())
                                .Build()));
                });
                x.RunAsLocalSystem()
                    .DependsOnEventLog()
                    .StartAutomatically()
                    .EnableServiceRecovery(rc => rc.RestartService(1));

                x.SetDescription("Checker Service");
                x.SetDisplayName("Checker Service");
                x.SetServiceName("CheckerService");
                x.UseNLog();
            });

        }
    }

    public class MyJob : IJob
    {
        private static Logger log = LogManager.GetCurrentClassLogger();

        public void Execute(IJobExecutionContext context)
        {
            log.Info("It is {0} and all is well", DateTime.UtcNow);
        }
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

需要添加对Topshelf.Quartz的引用!

我知道这是一件愚蠢而简单的事。