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. [cogs461] [网络流24题#10] 餐巾 [网络流,最小费用最大流]

    建图:从源点向第一层连边,第一层表示当天用掉多少餐巾,第二层表示当天需要多少餐巾,所以注意购买餐巾的边容量为无穷大,要从源点开始连向第二层的点,每天可能有剩余,在第一层内表示为流入第二天的节点.具体见 ...

  2. N天学习一个Linux命令之ln

    前言有时候同一个文件想创建多个别名,这个时候可以使用链接文件代替 用途对文件或者目录创建链接,默认创建的是硬链接 硬链接Linux底层文件系统由超级数据块,目录树对象,inode索引节点对象,文件对象 ...

  3. LDAP目录服务折腾之后的总结

    前言 公司管理员工信息以及组织架构的后台系统要和Active Directory目录服务系统打通,后台系统使用PHP开发, 折腾了二十多天,终于上线了,期间碰到过各种疑难问题,不过总算在GOOGLE大 ...

  4. 配置Chrome的代理服务器

    用命令行启动Chrome,带以下参数: --proxy-server="socks5://myproxy:8080"   下列参数可选 --host-resolver-rules= ...

  5. HTML5:控件自动获得焦点

    在HTML5中,页面打开后,需要指定的控件自动获得焦点很简单,只需要一个属性就可以实现 -  autofocus. 示例如下: <input type="text" auto ...

  6. java代理使用 apache ant实现文件压缩/解压缩

    [背景] 近日在研究web邮件下载功能,下载的邮件能够导入foxmail邮件client.可是批量下载邮件还需将邮件打成一个压缩包. 从网上搜索通过java实现文件压缩.解压缩有非常多现成的样例. [ ...

  7. nyoj 95

     #include<stdio.h> #include<queue> #include<algorithm> using namespace std; stru ...

  8. SpringMVC高速实现文件上传功能

    SpringMVC为我们封装了上传文件的功能,如今就试用一下 须要的jar包 我用的是Maven项目,就不须要到处下载Jar包了 SpringMVC的搭建 首先要在applicationContext ...

  9. mongoDB学习笔记——在C#中查询

    1.下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动貌似有很多种,如官方提供的samus. 实现思路大都类似.这里我们用官方提供的mongo-csh ...

  10. HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

    Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...