我正在使用使用 Quarz.net 3.7.0 的 .net 7 (.net core) 项目。 该项目配置了各种转换器:
builder.Services.AddControllers(options =>
{
//...
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new StringIdConverter());
options.JsonSerializerOptions.Converters.Add(new UshortIdConverter());
});
负责将各种 id 从对象转换为字符串或整数。我目前正在使用它,它对于 api 工作得很好。
我像这样注册 Quartz:
services.AddQuartz(q =>
{
q.SetProperty("quartz.serializer.type", "json");
q.UsePersistentStore(p =>
{
p.UseMySql(Environment.GetEnvironmentVariable("MYSQL_CONNECTION_STRING"));
});
});
// ASP.NET Core hosting
services.AddQuartzServer(options =>
{
options.AwaitApplicationStarted = true;
// when shutting down we want jobs to complete gracefully
options.WaitForJobsToComplete = true;
});
但是,当作业执行时,我遇到了异常:
Newtonsoft.Json.JsonSerializationException:无法找到用于 Common.Domain.Identity.UnitId 类型的构造函数。
我可以看到它正在尝试使用 Newtonsoft,但我希望它使用 System.Text.Json 序列化器。我已经安装了 Quartz.Serialization 包,我可以看到它具有 System.Text.Json 的功能: https://github.com/quartznet/quartznet/tree/main/src/Quartz.Serialization.SystemTextJson
如何配置 Quartz 以使用 System.Text.Json 而不是 Newtonsoft?
我终于找到答案了。 3.x.x 仅支持 Newtonsoft。我找到的 System.Text.Json 代码适用于版本 4