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

你可能感兴趣的文章
Java反射机制Reflection
查看>>
docker 暴露2375 端口。
查看>>
正确的类型转换
查看>>
第十四周实验报告:实验四 Android程序设计
查看>>
导出到word
查看>>
字符串的基本操作
查看>>
2013.10.21—2013.10.25周总结
查看>>
如何在makefile中写cd命令
查看>>
redis配置认证密码以及远程访问
查看>>
windons下一些软件的地址
查看>>
numpy ndarray 返回 index 问题
查看>>
leetcode 8. String to Integer (atoi)
查看>>
USACO section1.2 Name That Number
查看>>
Android 用application保存全局变量,关于Android中传递数据的一些讨论
查看>>
[Google Android] RelativeLayout 布局底部的EditText会被弹出的键盘遮挡
查看>>
(随用随总结)Linux下面的特殊权限&不同的文件类型
查看>>
Failed to start component [StandardEngine[Catalina].
查看>>
[VBA]批量新建指定名称的工作表
查看>>
委托与事件的关系
查看>>
固定资产管理系统 概要说明书说明书
查看>>