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<AppSettings>(Configuration);
services.Configure<InjectProperty>(options => { options.WriteText = "This is inject string"; });
services.AddQuartzHostedService()
.AddQuartzJob<HelloJob>()
.AddQuartzJob<InjectSampleJob>()
.AddQuartzJob<HelloJobSingle>()
.AddQuartzJob<InjectSampleJobSingle>();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions<AppSettings> settings)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
if (settings.Value.EnableHelloSingleJob)
{
app.UseQuartzJob<HelloJobSingle>(TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds().RepeatForever()))
.UseQuartzJob<InjectSampleJobSingle>(() =>
{
return TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds().RepeatForever());
});
}
if (settings.Value.EnableHelloJob)
{
app.UseQuartzJob<HelloJob>(new List<TriggerBuilder>
{
TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds().RepeatForever()),
TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds().RepeatForever())
}); app.UseQuartzJob<InjectSampleJob>(() =>
{
var result = new List<TriggerBuilder>();
result.Add(TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds().RepeatForever()));
return result;
});
}
} }
}

扩展方式示例:

public static class ModBusScheduler
{
public static void AddQuartzJobsService(this IServiceCollection services)
{
services.AddQuartzHostedService()
.AddQuartzJob<Slaver>("HAVC_Elev")
.AddQuartzJob<Slaver>("Lighting", "Lighting")
.AddQuartzJobDetail(() => JobBuilder.Create<Slaver>().WithIdentity("Meter").Build())
.AddQuartzJobDetail(() => JobBuilder.Create<Slaver>().WithIdentity("PowerDis").Build());
} public static void UserQuartzJobsService(this IApplicationBuilder app, AppSettings settings)
{
app.UseQuartzJob<Slaver>("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 连接  https://www.nuget.org/packages/QuartzHostedService/

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

Asp.Net Core 中利用QuartzHostedService 实现 Quartz 注入依赖 (DI)的更多相关文章

  1. ASP.NET Core中使用GraphQL - 第三章 依赖注入

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 SOL ...

  2. Asp.Net Core中利用Seq组件展示结构化日志功能

    在一次.Net Core小项目的开发中,掌握的不够深入,对日志记录并没有好好利用,以至于一出现异常问题,都得跑动服务器上查看,那时一度怀疑自己肯定没学好,不然这一块日志不可能需要自己扒服务器日志来查看 ...

  3. ASP.NET Core中使用Autofac进行属性注入

    一些无关紧要的废话: 作为一名双修程序员(自封的),喜欢那种使用Spring的注解形式进行依赖注入或者Unity的特性形式进行依赖注入,当然,形式大同小异,但结果都是一样的,通过属性进行依赖注入. A ...

  4. ASP.NET Core 中的SEO优化(4):自定义视图路径及主题切换

    系列回顾 <ASP.NET Core 中的SEO优化(1):中间件实现服务端静态化缓存> <ASP.NET Core 中的SEO优化(2):中间件中渲染Razor视图> < ...

  5. C# 嵌入dll 动软代码生成器基础使用 系统缓存全解析 .NET开发中的事务处理大比拼 C#之数据类型学习 【基于EF Core的Code First模式的DotNetCore快速开发框架】完成对DB First代码生成的支持 基于EF Core的Code First模式的DotNetCore快速开发框架 【懒人有道】在asp.net core中实现程序集注入

    C# 嵌入dll   在很多时候我们在生成C#exe文件时,如果在工程里调用了dll文件时,那么如果不加以处理的话在生成的exe文件运行时需要连同这个dll一起转移,相比于一个单独干净的exe,这种形 ...

  6. 【懒人有道】在asp.net core中实现程序集注入

    前言 在asp.net core中,我巨硬引入了DI容器,我们可以在不使用第三方插件的情况下轻松实现依赖注入.如下代码: // This method gets called by the runti ...

  7. ASP.NET Core中使用GraphQL - 最终章 Data Loader

    ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间 ...

  8. ASP.NET Core中使用GraphQL - 第四章 GraphiQL

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...

  9. ASP.NET Core中使用GraphQL - 第五章 字段, 参数, 变量

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...

随机推荐

  1. 算是帮华仔写的撸JSON文件,然后发到我的REST接口的PYTHON代码

    很久没有写过类似的代码了,感觉好陌生... #!/usr/bin/python #coding:utf-8 import json import getopt import sys import re ...

  2. ESXi License过期解决办法

    http://blog.sina.com.cn/s/blog_538439270101pqls.html

  3. [bzoj3192][JLOI2013]删除物品_树状数组_栈

    删除物品 bzoj-3192 JLOI-2013 题目大意:给你n个物品,分成2堆.所有的物品有不同的优先级.我只可以将一堆中的堆顶移动到另一个堆的堆顶.而如果当前物品是全局所有物品中优先级最高的,我 ...

  4. js移除style属性

    这个属性是通过   div.style.color="red"   这种类似添加的,想要添加重置函数,使用div.removeAttribute("style" ...

  5. 【转】php ob_start()、ob_end_flush和ob_end_clean()多级缓冲

    原文:https://my.oschina.net/CuZn/blog/68650 当php.ini配置文件中的  设置开启的时候,就相当于PHP已经打开了最顶层的 一级缓存 (等价于调用了一次 ob ...

  6. 2.4-EN_STP

    2.4-EN_STP     增强型生成树协议(EN_STP): Spannig Tree port states: blocking 20s+listening 15s+learning 15s最后 ...

  7. 使用百度地图API进行Android地图应用开发(Eclipse)

    随着基于位置的服务的兴起,地图类App呈现爆发趋势.随着而来的是地图供应商开放大量的API.供开发人员开发基于PC或者移动端的应用程序. 如今我们研究使用百度地图SDK进行Android项目的开发. ...

  8. QFileDialog关于选择文件对话框中的几个信号的说明(currentChanged,directoryEntered,fileSelected,filterSelected)

    QFileDialog关于选择文件对话框中的几个信号 实例: openFile::openFile(QWidget *parent) :QWidget(parent),ui(new Ui::openF ...

  9. hdu 1542(线段树+扫描线 求矩形相交面积)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  10. 【POJ 3322】 Bloxorz I

    [题目链接] http://poj.org/problem?id=3322 [算法] 广度优先搜索 [代码] #include <algorithm> #include <bitse ...