gpt4 book ai didi

c# - 使用 Topshelf 和 Quartz.NET

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:02 24 4
gpt4 key购买 nike

我在设置基本的 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 配置?

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

最佳答案

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

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

关于c# - 使用 Topshelf 和 Quartz.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44899506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com