如何将石英网计划从服务收集到Crystalquartz

发布于 2025-01-23 05:38:48 字数 227 浏览 2 评论 0原文

我正在尝试使用.NET 5使用石英网和Crystalquartz。 我将调度程序添加到Services Collection中,并使用addquartz

如何从内部的服务集合中获取调度程序?

services.AddQuartz(q =>{

...

q.SchedulerId = "MyScheduler ID";

...

})

I'm trying to use Quartz NET and CrystalQuartz with .NET 5.
I add the scheduler to services collection with AddQuartz

How could I get the scheduler from the services collection within?

services.AddQuartz(q =>{

...

q.SchedulerId = "MyScheduler ID";

...

})

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

も星光 2025-01-30 05:38:48

作为第一步,您需要安装Crystalquartz.aspnetcore nuget软件包到目标项目。

在下一步中,您需要将Crystalquartz中间件挂在ASP.NET核心环境中。应该在管道初始化时调用usecrystalquartz扩展方法。面板配置的通用语法看起来像:

 app.UseCrystalQuartz(() => scheduler, options);

调度程序是您的Ischeduler(本地或远程)。

您应该已经有一个iScheduler对象实例,因此您可以将其指向其指向func将其传递给配置扩展方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // ...

    IScheduler scheduler = CreateScheduler();
    app.UseCrystalQuartz(() => scheduler);

    // ...
}

// this method is just a sample of scheduler initialization
private IScheduler CreateScheduler()
{
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler().Result;

    // construct job info
    var jobDetail = JobBuilder.Create<HelloJob>()
        .WithIdentity("myJob")
        .StoreDurably()
        .Build();

    // fire every minute
    var trigger = TriggerBuilder.Create()
        .WithIdentity("myTrigger")
        .StartNow()
        .WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
        .Build();

    scheduler.ScheduleJob(jobDetail, trigger);

    scheduler.Start();

    return scheduler;
}

As a first step, you need to install CrystalQuartz.AspNetCore NuGet package to the target project.

As a next step, you need to hook CrystalQuartz middleware into your ASP.NET Core environment. It should be done by calling UseCrystalQuartz extension method at the moment of pipeline initialization. The generic syntax for panel configuration looks like this:

 app.UseCrystalQuartz(() => scheduler, options);

scheduler is your IScheduler (local or remote).

You should already have an ISchedulerobject instance so you can pass a Func pointing to it to the configuration extension method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // ...

    IScheduler scheduler = CreateScheduler();
    app.UseCrystalQuartz(() => scheduler);

    // ...
}

// this method is just a sample of scheduler initialization
private IScheduler CreateScheduler()
{
    var schedulerFactory = new StdSchedulerFactory();
    var scheduler = schedulerFactory.GetScheduler().Result;

    // construct job info
    var jobDetail = JobBuilder.Create<HelloJob>()
        .WithIdentity("myJob")
        .StoreDurably()
        .Build();

    // fire every minute
    var trigger = TriggerBuilder.Create()
        .WithIdentity("myTrigger")
        .StartNow()
        .WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
        .Build();

    scheduler.ScheduleJob(jobDetail, trigger);

    scheduler.Start();

    return scheduler;
}
墨落画卷 2025-01-30 05:38:48
app.UseCrystalQuartz(() => app.Services.GetRequiredService<ISchedulerFactory>().GetScheduler());
app.UseCrystalQuartz(() => app.Services.GetRequiredService<ISchedulerFactory>().GetScheduler());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文