2017-07-04 48 views
1

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

出现错误:

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?) 

我想我缺少明显的东西在这里...

方问题:有没有更简单设立一个石英/ Topshelf配置方式?

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); 
 
     } 
 
    } 
 
}

回答

1

需要在一个引用添加到Topshelf.Quartz!

我知道这是愚蠢和简单的事情。