Files
SilkierQuartz/sample2/Program.cs
2022-04-24 19:07:38 +08:00

117 lines
4.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Quartz;
using SilkierQuartz;
using SilkierQuartz.Example;
using SilkierQuartz.Example.Jobs;
using System.Configuration;
using WebApplication1.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddRazorPages();
var services = builder.Services;
var Configuration =builder.Configuration;
services.AddSilkierQuartz(options =>
{
options.VirtualPathRoot = "/quartz";
options.UseLocalTime = true;
options.DefaultDateFormat = "yyyy-MM-dd";
options.DefaultTimeFormat = "HH:mm:ss";
options.CronExpressionOptions = new CronExpressionDescriptor.Options()
{
DayOfWeekStartIndexZero = false //Quartz uses 1-7 as the range
};
}
#if ENABLE_AUTH
,
authenticationOptions =>
{
authenticationOptions.AuthScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authenticationOptions.SilkierQuartzClaim = "Silkier";
authenticationOptions.SilkierQuartzClaimValue = "Quartz";
authenticationOptions.UserName = "admin";
authenticationOptions.UserPassword = "password";
authenticationOptions.AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowOnlyUsersWithClaim;
}
#else
,
authenticationOptions =>
{
authenticationOptions.AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowAnonymous;
}
#endif
);
services.AddOptions();
services.Configure<AppSettings>(Configuration);
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
services.AddQuartzJob<HelloJob>()
.AddQuartzJob<InjectSampleJob>()
.AddQuartzJob<HelloJobSingle>()
.AddQuartzJob<InjectSampleJobSingle>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseMigrationsEndPoint();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSilkierQuartz();
app.MapRazorPages();
#region <EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> SilkierQuartzAttribe <EFBFBD><EFBFBD><EFBFBD>ԵĽ<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD>IJob<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>UseQuartzJob<EFBFBD><EFBFBD>IJob<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ConfigureServices<EFBFBD><EFBFBD><EFBFBD><EFBFBD>AddQuartzJob
app.UseQuartzJob<HelloJobSingle>(TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()))
.UseQuartzJob<InjectSampleJobSingle>(() =>
{
return TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever());
});
app.UseQuartzJob<HelloJob>(new List<TriggerBuilder>
{
TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()),
TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever()),
//Add a sample that uses 1-7 for dow
TriggerBuilder.Create()
.WithCronSchedule("0 0 2 ? * 7 *"),
});
app.UseQuartzJob<InjectSampleJob>(() =>
{
var result = new List<TriggerBuilder>();
result.Add(TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever()));
return result;
});
#endregion
app.Run();