如何在ASP.NET Web API Core应用程序上使用QUARTZ实现调度程序?

时间:2016-06-23 17:38:16

标签: c# asp.net asp.net-web-api asp.net-core quartz-scheduler

我目前正在开发一个处理约会和重复活动的Web API。我发现quartz-scheduler.net是合适的,但问题是它与asp.NET Core版本不兼容。

有没有办法将quartz.NET实现到asp.NET核心,还是有某种替代或替代?

2 个答案:

答案 0 :(得分:3)

Quartz.NET 3.0 Alpha 1发布:支持.NET Core / netstandard 1.3。 请参阅此announcement

您可以监控此故障单中的进度:Support for .NET Core or vNext #355

答案 1 :(得分:0)

我设法按照以下步骤进行:

  1. 创建.NET核心服务扩展(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);
        }
    }
    
  2. 在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
        }
    }
    
  3. 现在每当我想使用调度程序对象时,我都可以使用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
            ...
        }
    }
    
  4. PS1:我使用的所有内容都来自this questionthis answer

    PS2:不要忘记创建localdb数据库并按照quartznet here

    给出的脚本填充它