【发布时间】:2016-06-23 17:38:16
【问题描述】:
我目前正在开发一个处理约会和重复事件的 Web API。我发现quartz-scheduler.net 是合适的,但问题是它与asp.NET Core 版本不兼容。
有没有办法将quartz.NET 实现到asp.NET 核心,或者是否有某种替代或替代方案?
【问题讨论】:
标签: c# asp.net asp.net-web-api asp.net-core quartz-scheduler
我目前正在开发一个处理约会和重复事件的 Web API。我发现quartz-scheduler.net 是合适的,但问题是它与asp.NET Core 版本不兼容。
有没有办法将quartz.NET 实现到asp.NET 核心,或者是否有某种替代或替代方案?
【问题讨论】:
标签: c# asp.net asp.net-web-api asp.net-core quartz-scheduler
Quartz.NET 3.0 Alpha 1 发布:支持 .NET Core / netstandard 1.3。 在announcement 中查看更多信息。
您可以在此工单中监控进度:Support for .NET Core or vNext #355
【讨论】:
我按照以下步骤设法做到了:
创建 .NET Core 服务扩展 (QuartzExtension.cs):
public static class QuartzExtensions
{
public static void UseQuartz(this IApplicationBuilder app)
{
app.ApplicationServices.GetService<IScheduler>();
}
public static async void AddQuartz(this IServiceCollection services)
{
var properties = new NameValueCollection
{
// json serialization is the one supported under .NET Core (binary isn't)
["quartz.serializer.type"] = "json",
// the following setup of job store is just for example and it didn't change from v2
["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
["quartz.jobStore.useProperties"] = "false",
["quartz.jobStore.dataSource"] = "default",
["quartz.jobStore.tablePrefix"] = "QRTZ_",
["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
["quartz.dataSource.default.provider"] = "SqlServer-41", // SqlServer-41 is the new provider for .NET Core
["quartz.dataSource.default.connectionString"] = @"Server=(localdb)\MSSQLLocalDB;Database=sta-scheduler-quartz;Integrated Security=true"
};
var schedulerFactory = new StdSchedulerFactory(properties);
var scheduler = schedulerFactory.GetScheduler().Result;
scheduler.Start().Wait();
services.AddSingleton<IScheduler>(scheduler);
}
}
在 Startup.cs 中使用如下
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Authentication service
JwtAuthentication.AddJwtAuthentication(services);
services.AddQuartz(); // <======== THIS LINE
services.AddSingleton<IHttpRequestScheduler, HttpRequestScheduler>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
IApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseAuthentication();
// Add JWT generation endpoint:
var options = new TokenProviderOptions
{
Audience = "ExampleAudience",
Issuer = "ExampleIssuer",
SigningCredentials = new SigningCredentials(JwtAuthentication.SIGNING_KEY, SecurityAlgorithms.HmacSha256),
};
app.UseMiddleware<TokenProviderMiddleware>(Options.Create(options));
app.UseQuartz(); // <======== THIS LINE
}
}
现在,每当我想使用调度程序对象时,我都可以使用 DependecyInjection 来获取它,就像这个例子:
public class HttpRequestScheduler : IHttpRequestScheduler
{
private IScheduler _scheduler;
public HttpRequestScheduler(IScheduler scheduler)
{
_scheduler = scheduler;
}
public void Schedule()
{
// Whatever you want to do with the scheduler
...
}
}
PS1:我使用的所有东西都是从this question和this answer获得的。
PS2:不要忘记创建您的 localdb 数据库并按照quartznet here 提供的脚本填充它
【讨论】: