Quartz 3.4在.NET 6 Web应用程序中运行的IIS 10的作业直到第一个Web请求才会启动

发布于 2025-01-21 10:21:09 字数 3283 浏览 3 评论 0原文

在.NET 6 Web应用程序中,我添加了一个石英作业来运行日常任务。使用.addquartz和.addquartzhostedservice添加了石英作业“ rel =“ nofollow noreferrer”> https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/hosted-services-integration.html#installation 页面

我该怎么做才能做些什么来做些什么来做些什么来做些什么来做些什么当应用程序池启动时,石英调度程序/作业开始?

在向应用程序提出第一个Web请求后,石英确实开始正确运行。在IIS中,我将AppPools启动模式设置为ElwardSrumn,.NET CLR版本既不托管代码或v4.0。我在Web服务器上安装了应用程序初始化,并将启用属性的应用程序设置为true。

IIS启动时,W3WP.EXE任务开始使用应用程序池AD身份。在第一个Web请求之后,新的ConHost.exe任务在Web服务器上也开始使用应用程序池AD标识,我假设它是Quartz Scheduler运行的。 我检查了事件查看器是否有错误,但我找不到与此问题有关的任何内容。

这是Web应用程序的program.cs。

using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.EntityFrameworkCore;
using DataChecker_3186.Data;
using DataChecker_3186.Models;
using Quartz;
using DataChecker_3186.IdmsInterface;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<FitProContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("FitProContext")));
builder.Services.AddDbContext<HITSContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("HITSContext")));

builder.Services.AddScoped<ILookupLists, LookupLists>();

builder.Services.AddControllersWithViews();

builder.Services.AddRazorPages();

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});

builder.Services.AddTransient<IIdmsService, IdmsService>();

var sendJobKey = new JobKey("SendToIdms");
var watchJobKey = new JobKey("MonitorIdmsOutput");
var sendToIdmsCron = builder.Configuration.GetValue<string>("IdmsInterfaceSettings:SendToIdmsCron");
var monitorIdmsOutputCron = builder.Configuration.GetValue<string>("IdmsInterfaceSettings:MonitorIdmsOutputCron");

builder.Services.AddQuartz(q =>
{
    q.SchedulerId = "MaskFitRecordsJobScheduler";
    q.SchedulerName = "MaskFitRecords Job Scheduler";
    q.UseMicrosoftDependencyInjectionJobFactory();
    q.AddJob<SendToIdmsJob>(opts => opts.WithIdentity(sendJobKey));
    q.AddJob<ProcessIdmsOutput>(opts => opts.WithIdentity(watchJobKey));

    q.AddTrigger(opts => opts
        .WithIdentity(sendJobKey.Name + "-trigger")
        .ForJob(sendJobKey)
        .StartNow()
        .WithCronSchedule(sendToIdmsCron));

    q.AddTrigger(opts => opts
        .ForJob(watchJobKey)
        .WithIdentity(watchJobKey.Name + "-trigger")
        .WithCronSchedule(monitorIdmsOutputCron));
});

builder.Services.AddTransient<SendToIdmsJob>();
builder.Services.AddTransient<ProcessIdmsOutput>();

builder.Services.AddQuartzHostedService(q =>
    {
        q.WaitForJobsToComplete = true;
    });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=FitTests}/{action=Index}/{id?}");

app.Run();

In a .NET 6 Web application I added a Quartz Job to run a daily task. The quartz job is added using .AddQuartz and .AddQuartzHostedService as shown on https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/hosted-services-integration.html#installation page

What can I do to make the Quartz Scheduler/Jobs start when the application pool starts?

Quartz does start running correctly after the first web request is made to the application. In IIS I have set the AppPools Start Mode to AlwaysRunning,the .NET CLR Version to both No Managed Code or v4.0. I installed Application Initialization on the web server and set the applications Preload Enabled property to True.

When IIS starts up, a w3wp.exe task starts using the application pools AD identity. After the first web request, a new conhost.exe task starts on the web server also using the application pools AD identity which I assume is the Quartz scheduler running.
I checked the Event Viewer for errors and I am not finding anything that appears to be related to this issue.

This is the Program.cs of the web app.

using Microsoft.AspNetCore.Authentication.Negotiate;
using Microsoft.EntityFrameworkCore;
using DataChecker_3186.Data;
using DataChecker_3186.Models;
using Quartz;
using DataChecker_3186.IdmsInterface;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<FitProContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("FitProContext")));
builder.Services.AddDbContext<HITSContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("HITSContext")));

builder.Services.AddScoped<ILookupLists, LookupLists>();

builder.Services.AddControllersWithViews();

builder.Services.AddRazorPages();

builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
   .AddNegotiate();

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});

builder.Services.AddTransient<IIdmsService, IdmsService>();

var sendJobKey = new JobKey("SendToIdms");
var watchJobKey = new JobKey("MonitorIdmsOutput");
var sendToIdmsCron = builder.Configuration.GetValue<string>("IdmsInterfaceSettings:SendToIdmsCron");
var monitorIdmsOutputCron = builder.Configuration.GetValue<string>("IdmsInterfaceSettings:MonitorIdmsOutputCron");

builder.Services.AddQuartz(q =>
{
    q.SchedulerId = "MaskFitRecordsJobScheduler";
    q.SchedulerName = "MaskFitRecords Job Scheduler";
    q.UseMicrosoftDependencyInjectionJobFactory();
    q.AddJob<SendToIdmsJob>(opts => opts.WithIdentity(sendJobKey));
    q.AddJob<ProcessIdmsOutput>(opts => opts.WithIdentity(watchJobKey));

    q.AddTrigger(opts => opts
        .WithIdentity(sendJobKey.Name + "-trigger")
        .ForJob(sendJobKey)
        .StartNow()
        .WithCronSchedule(sendToIdmsCron));

    q.AddTrigger(opts => opts
        .ForJob(watchJobKey)
        .WithIdentity(watchJobKey.Name + "-trigger")
        .WithCronSchedule(monitorIdmsOutputCron));
});

builder.Services.AddTransient<SendToIdmsJob>();
builder.Services.AddTransient<ProcessIdmsOutput>();

builder.Services.AddQuartzHostedService(q =>
    {
        q.WaitForJobsToComplete = true;
    });

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=FitTests}/{action=Index}/{id?}");

app.Run();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文