首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Topshelf和Quartz.NET

使用Topshelf和Quartz.NET
EN

Stack Overflow用户
提问于 2017-07-04 15:23:30
回答 1查看 2K关注 0票数 2

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

访问ScheduleQuartzJob时出现错误:

代码语言:javascript
复制
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配置?

代码语言:javascript
复制
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);
        }
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-04 15:54:32

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44899506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档