2

I am currently developing a Web API that processes appointments and recurring events. I found quartz-scheduler.net that would be appropriate but the problem is that it is not compatible with asp.NET Core version.

Is there a way to implement quartz.NET to asp.NET core or is there some kind of replacement or alternative?

Mariane Bacha
  • 108
  • 1
  • 10

2 Answers2

3

Quartz.NET 3.0 Alpha 1 Released: supports .NET Core / netstandard 1.3. see more in this announcement.

You can monitor the progress in this ticket: Support for .NET Core or vNext #355

hbulens
  • 1,872
  • 3
  • 24
  • 45
Set
  • 47,577
  • 22
  • 132
  • 150
0

I managed to do it following these steps:

  1. Created .NET Core Service Extension (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. Used it as follow in 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. Now whenever I want to use the scheduler object I can get it with DependecyInjection, like this example:

    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: Everything I used was obtained from this question and this answer.

PS2: Do not forget to create your localdb database and populate it following the scripts given by quartznet here