【问题标题】:How to implement a scheduler using QUARTZ on an ASP.NET Web API Core application?如何在 ASP.NET Web API Core 应用程序上使用 QUARTZ 实现调度程序?
【发布时间】: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


    【解决方案1】:

    Quartz.NET 3.0 Alpha 1 发布:支持 .NET Core / netstandard 1.3。 在announcement 中查看更多信息。

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

    【讨论】:

      【解决方案2】:

      我按照以下步骤设法做到了:

      1. 创建 .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);
            }
        }
        
      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
                ...
            }
        }
        

      PS1:我使用的所有东西都是从this questionthis answer获得的。

      PS2:不要忘记创建您的 localdb 数据库并按照quartznet here 提供的脚本填充它

      【讨论】:

        猜你喜欢
        • 2017-09-18
        • 2018-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多