使用 Quartz.net 时无法加载类型 System.Configuration.ConfigurationManager
Could not load type System.Configuration.ConfigurationManager while using Quartz.net
我刚刚开始使用 Quartz.NET 调度程序。我只是从 https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/using-quartz.html 复制代码来检查工作。
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
// construct a scheduler factory using defaults
StdSchedulerFactory factory = new StdSchedulerFactory();
// get a scheduler
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
}
但即使在显式添加 System.Configuration.ConfigurationManager
dll 后,我仍收到以下消息。
System.Configuration.ConfigurationManager in the System.TypeLoadException
HResult=0x80131522
Message=Could not load type 'System.Configuration.ConfigurationManager' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Source=Quartz
StackTrace:
at Quartz.Util.Configuration.GetSection(String sectionName)
at Quartz.Impl.StdSchedulerFactory.InitializeProperties(ILog logger, Boolean throwOnProblem)
at Quartz.Impl.StdSchedulerFactory.Initialize()
at Quartz.Impl.StdSchedulerFactory.<GetScheduler>d__69.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Scheduler.Program.<Main>d__0.MoveNext()
这是我的解决方案的目录结构。
请有人建议我如何解决这个问题。
您引用的 DLL system.configuration.configurationmanager.5.0.0.nupkg\lib\net461\
是为 .NET 版本 4.6.1 构建的。
不幸的是,您的应用程序针对 .NET 版本 5 - 它基于 .NET Core 实现,如 official docs.
中所述
分辨率应该是:
- 引用
System.Configuration.ConfigurationManager
到 DLL 版本 .NET Core 或 .NET 5.
- 或将应用程序的基础更改为 .NET Framework 4.6.2 及更高版本(必须是 .NET FW)
我刚刚开始使用 Quartz.NET 调度程序。我只是从 https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/using-quartz.html 复制代码来检查工作。
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
// construct a scheduler factory using defaults
StdSchedulerFactory factory = new StdSchedulerFactory();
// get a scheduler
IScheduler scheduler = await factory.GetScheduler();
await scheduler.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
}
但即使在显式添加 System.Configuration.ConfigurationManager
dll 后,我仍收到以下消息。
System.Configuration.ConfigurationManager in the System.TypeLoadException
HResult=0x80131522
Message=Could not load type 'System.Configuration.ConfigurationManager' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Source=Quartz
StackTrace:
at Quartz.Util.Configuration.GetSection(String sectionName)
at Quartz.Impl.StdSchedulerFactory.InitializeProperties(ILog logger, Boolean throwOnProblem)
at Quartz.Impl.StdSchedulerFactory.Initialize()
at Quartz.Impl.StdSchedulerFactory.<GetScheduler>d__69.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Scheduler.Program.<Main>d__0.MoveNext()
这是我的解决方案的目录结构。
请有人建议我如何解决这个问题。
您引用的 DLL system.configuration.configurationmanager.5.0.0.nupkg\lib\net461\
是为 .NET 版本 4.6.1 构建的。
不幸的是,您的应用程序针对 .NET 版本 5 - 它基于 .NET Core 实现,如 official docs.
中所述分辨率应该是:
- 引用
System.Configuration.ConfigurationManager
到 DLL 版本 .NET Core 或 .NET 5. - 或将应用程序的基础更改为 .NET Framework 4.6.2 及更高版本(必须是 .NET FW)