Asp.Net Core 中利用QuartzHostedService 实现 Quartz 注入依赖 (DI)
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)的更多相关文章
- ASP.NET Core中使用GraphQL - 第三章 依赖注入
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 SOL ...
- Asp.Net Core中利用Seq组件展示结构化日志功能
在一次.Net Core小项目的开发中,掌握的不够深入,对日志记录并没有好好利用,以至于一出现异常问题,都得跑动服务器上查看,那时一度怀疑自己肯定没学好,不然这一块日志不可能需要自己扒服务器日志来查看 ...
- ASP.NET Core中使用Autofac进行属性注入
一些无关紧要的废话: 作为一名双修程序员(自封的),喜欢那种使用Spring的注解形式进行依赖注入或者Unity的特性形式进行依赖注入,当然,形式大同小异,但结果都是一样的,通过属性进行依赖注入. A ...
- ASP.NET Core 中的SEO优化(4):自定义视图路径及主题切换
系列回顾 <ASP.NET Core 中的SEO优化(1):中间件实现服务端静态化缓存> <ASP.NET Core 中的SEO优化(2):中间件中渲染Razor视图> < ...
- 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,这种形 ...
- 【懒人有道】在asp.net core中实现程序集注入
前言 在asp.net core中,我巨硬引入了DI容器,我们可以在不使用第三方插件的情况下轻松实现依赖注入.如下代码: // This method gets called by the runti ...
- ASP.NET Core中使用GraphQL - 最终章 Data Loader
ASP.NET Core中使用GraphQL - 目录 ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间 ...
- ASP.NET Core中使用GraphQL - 第四章 GraphiQL
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...
- ASP.NET Core中使用GraphQL - 第五章 字段, 参数, 变量
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...
随机推荐
- java 9 Spring Cloud @EnableEurekaServer javax.xml.bind.JAXBContext not present
java 9 Spring Cloud @EnableEurekaServer javax.xml.bind.JAXBContext not present jdk 8下面还可以正常启动,jdk9 ...
- java调用百度地图API依据地理位置中文获取经纬度
百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...
- Cookie && Session && Token
Cookies Cookie的由来: HTTP 本身是一个无状态的 request/response 协议. server接收一个来自client的request, 处理完以后返回一个response ...
- C++ 移位运算与进制转换 浅析
移位运算包括"逻辑移位"(logical shift)和"算术移位"(arithmetic shift). 逻辑移位:移出去的位丢弃,空缺位(vacant bi ...
- 从零開始学Xamarin.Forms(二) 环境搭建、创建项目
一.环境搭建 Windows下环境搭建: 1.下载并安装jdk.Android SDK和NDK.当然还须要 VS2013 update 2(VS2010.VS2012均可)以上. a. 最新 ...
- android Service not registered
Caused by: java.lang.IllegalArgumentException: Service not registered:com.broadcom.bt.app.settings.S ...
- hdu4738Caocao's Bridges
什么?有人要炸我的桥?!D飞他(心疼周瑜大都督) 这个就是求割边/桥了. #include<cstdio> #include<iostream> #include<cst ...
- bazel编译tensorflow 生成libtensorflow_inference.so 和 libandroid_tensorflow_inference_java.jar
bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so --crosstool_top=//externa ...
- SQL Server2012 T-SQL基础教程--读书笔记(5-7章)
SQL Server2012 T-SQL基础教程--读书笔记(5-7章) SqlServer T-SQL 示例数据库:点我 Chapter 05 表表达式 5.1 派生表 5.1.1 分配列别名 5. ...
- Balloons(DFS)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2248 题意:(1)求图中四连块(有公共边的方块 ...