birzha-contracts/Program.cs

58 lines
1.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using birzha_contracts.Jobs;
using Microsoft.Extensions.Configuration;
using birzha_contracts.Managers;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<MvcContractContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MvcContractContext") ?? throw new InvalidOperationException("Connection string 'MvcContractContext' not found.")));
var time = ConfigManager.AppSetting["UpdateTime"];
builder.Services.AddControllersWithViews();
builder.Services.AddQuartz(q =>
{
q.UseMicrosoftDependencyInjectionJobFactory();
var jobKey = new JobKey("HelloWorldJob");
var updateJobKey = new JobKey("UpdateJob");
q.AddJob<HelloWorldJob>(opts => opts.WithIdentity(jobKey));
q.AddJob<UpdateJob>(opts => opts.WithIdentity(updateJobKey));
q.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity("HelloWorldJob-trigger")
.WithCronSchedule(time));
q.AddTrigger(opts => opts
.ForJob(updateJobKey)
.WithIdentity("UpdateJob-trigger")
.WithCronSchedule(time));
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();