博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net Core 中利用QuartzHostedService 实现 Quartz 注入依赖 (DI)
阅读量:4948 次
发布时间:2019-06-11

本文共 3763 字,大约阅读时间需要 12 分钟。

QuartzHostedService  是一个用来在Asp.Net Core 中实现 Quartz 的任务注入依赖的nuget 包:

基本示例如下:

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using AspNetCoreSampleQuartzHostedService.Jobs;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using Microsoft.Extensions.Options;using Quartz;using QuartzHostedService;namespace AspNetCoreSampleQuartzHostedService{    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().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);            services.AddOptions();            services.Configure
(Configuration); services.Configure
(options => { options.WriteText = "This is inject string"; }); services.AddQuartzHostedService() .AddQuartzJob
() .AddQuartzJob
() .AddQuartzJob
() .AddQuartzJob
(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions
settings) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); if (settings.Value.EnableHelloSingleJob) { app.UseQuartzJob
(TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())) .UseQuartzJob
(() => { return TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()); }); } if (settings.Value.EnableHelloJob) { app.UseQuartzJob
(new List
{ TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()), TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever()) }); app.UseQuartzJob
(() => { var result = new List
(); result.Add(TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())); return result; }); } } }}

 

扩展方式示例:

public static class ModBusScheduler    {        public static void AddQuartzJobsService(this IServiceCollection services)        {            services.AddQuartzHostedService()                    .AddQuartzJob
("HAVC_Elev") .AddQuartzJob
("Lighting", "Lighting") .AddQuartzJobDetail(() => JobBuilder.Create
().WithIdentity("Meter").Build()) .AddQuartzJobDetail(() => JobBuilder.Create
().WithIdentity("PowerDis").Build()); } public static void UserQuartzJobsService(this IApplicationBuilder app, AppSettings settings) { app.UseQuartzJob
("HAVC_Elev", () => { return TriggerBuilder.Create() .WithIdentity("HAVC_Elev") .UsingJobData("modbus", settings.AC_ElevatorSlave) .UsingJobData("devicetype", "HAVC_Elev") .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever()).StartNow(); });}

  

nuget 连接  

github地址  https://github.com/mukmyash/QuartzHostedService

 

转载于:https://www.cnblogs.com/MysticBoy/p/11003030.html

你可能感兴趣的文章
浮点数内存如何存储的
查看>>
JsonCpp 的使用
查看>>
问题账户需求分析
查看>>
hp 服务器通过串口重定向功能的使用
查看>>
此博客不再发表对自己私事的看法
查看>>
导致Asp.Net站点重启的10个原因
查看>>
【PMP】Head First PMP 学习笔记 第一章 引言
查看>>
抓住云机遇编排工作 搞定复杂IT工作流
查看>>
MYSQL的longtext字段能放多少数据?
查看>>
MTK 平台上如何给 camera 添加一种 preview size
查看>>
mysql定时备份自动上传
查看>>
17岁时少年决定把海洋洗干净,现在21岁的他做到了
查看>>
《写给大忙人看的java se 8》笔记
查看>>
倒计时:计算时间差
查看>>
Linux/windows P2V VMWare ESXi
查看>>
Windows XP倒计时到底意味着什么?
查看>>
运维工程师在干什么学些什么?【致菜鸟】
查看>>
Linux中iptables详解
查看>>
java中回调函数以及关于包装类的Demo
查看>>
maven异常:missing artifact jdk.tools:jar:1.6
查看>>