Core开发-后台任务利器Hangfire使用
Core开发-后台任务利器Hangfire使用
ASP.NET Core开发系列之后台任务利器Hangfire 使用。
Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/任务计划程序。
可以使用于ASP.NET 应用也可以使用于控制台。Hangfire 只需简单几句代码即可创建新的不同种类的任务。
目前 Hangfire 已经支持.NET Core ,现在就给大家讲解下在ASP.NET Core 里的使用。
Hangfire GitHub:https://github.com/HangfireIO/Hangfire
官网:http://hangfire.io/
相关文档介绍:http://docs.hangfire.io/en/latest/
首先我们新建一个ASP.NET Core Web Application
选择模板-》空的模板
然后添加引用:
NuGet 命令行执行
Install-Package Hangfire
添加好引用以后我们就可以来使用。
打开Startup.cs
首先在ConfigureServices 方法中注册服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(r=>r.UseSqlServerStorage("Data Source=.;Initial Catalog=HangfireDemo;User ID=sa;Password=123456"));
}
这里是配置数据库,数据库需要确保存在,这里配置的是SQL Server数据库,目前官方支持SQL Server。
然后在Configure 方法中加入HangfireServer及HangfireDashboard:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(); if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseHangfireServer();
app.UseHangfireDashboard(); app.Run(context =>
{
return context.Response.WriteAsync("Hello from ASP.NET Core!");
});
}

然后选择Kestrel执行,访问地址:http://localhost:5000/hangfire
成功运行,下面我们就可以来添加任务了。

app.Map("/index", r =>
{
r.Run(context =>
{
//任务每分钟执行一次
RecurringJob.AddOrUpdate(() => Console.WriteLine($"ASP.NET Core LineZero"), Cron.Minutely());
return context.Response.WriteAsync("ok");
});
}); app.Map("/one", r =>
{
r.Run(context =>
{
//任务执行一次
BackgroundJob.Enqueue(() => Console.WriteLine($"ASP.NET Core One Start LineZero{DateTime.Now}"));
return context.Response.WriteAsync("ok");
});
}); app.Map("/await", r =>
{
r.Run(context =>
{
//任务延时两分钟执行
BackgroundJob.Schedule(() => Console.WriteLine($"ASP.NET Core await LineZero{DateTime.Now}"), TimeSpan.FromMinutes(2));
return context.Response.WriteAsync("ok");
});
});

这里创建任务只是为了方便,我们也可以在初始化的时候创建,也可以在controller 中创建。
下面我们来执行。
首先访问 http://localhost:5000/index
然后访问 http://localhost:5000/await
最后访问 http://localhost:5000/one
这样任务也就都执行起来了。
Jobs 也就是查看所有的任务,我们可以在节目界面操作运行及删除,很方便。
我们还可以点击任务,查看任务详情。以及任务执行结果。
最终运行一段时间,还有图表展示
Startup.cs 完整代码:
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(r=>r.UseSqlServerStorage("Data Source=.;Initial Catalog=HangfireDemo;User ID=sa;Password=123456"));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//GlobalConfiguration.Configuration.UseSqlServerStorage("Data Source=.;Initial Catalog=OrchardCMS;User ID=sa;Password=sa123456");
app.UseHangfireServer();
app.UseHangfireDashboard();
app.Map("/index", r =>
{
r.Run(context =>
{
//任务每分钟执行一次
RecurringJob.AddOrUpdate(() => Console.WriteLine($"ASP.NET Core LineZero"), Cron.Minutely());
return context.Response.WriteAsync("ok");
});
});
app.Map("/one", r =>
{
r.Run(context =>
{
//任务执行一次
BackgroundJob.Enqueue(() => Console.WriteLine($"ASP.NET Core One Start LineZero{DateTime.Now}"));
return context.Response.WriteAsync("ok");
});
});
app.Map("/await", r =>
{
r.Run(context =>
{
//任务延时两分钟执行
BackgroundJob.Schedule(() => Console.WriteLine($"ASP.NET Core await LineZero{DateTime.Now}"), TimeSpan.FromMinutes(2));
return context.Response.WriteAsync("ok");
});
});
app.Run(context =>
{
return context.Response.WriteAsync("Hello from ASP.NET Core!");
});
}
}
通过Hangfire, 这样我们就可以很方便的在ASP.NET Core 里创建后台任务。而且提供管理界面供我们操作。
Core开发-后台任务利器Hangfire使用的更多相关文章
- ASP.NET Core开发-后台任务利器Hangfire使用
ASP.NET Core开发系列之后台任务利器Hangfire 使用. Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/任务计划程序. 可以使用于ASP.NET 应用也 ...
- Visual Studio 2017 ASP.NET Core开发
Visual Studio 2017 ASP.NET Core开发,Visual Studio 2017 已经内置ASP.NET Core 开发工具. 在选择.NET Core 功能安装以后就可以进行 ...
- NET Core开发
NET Core开发 Visual Studio 2017 ASP.NET Core开发,Visual Studio 2017 已经内置ASP.NET Core 开发工具. 在选择.NET Core ...
- ASP.NET Core-组件-后台任务:Hangfire
ylbtech-ASP.NET Core-组件-后台任务:Hangfire Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 完善与美化,Swagger登场
上一篇文章(https://www.cnblogs.com/meowv/p/12896898.html)已经成功将博客项目跑起来了,那么本篇主要是将之前遗留的问题解决,现在的代码看起来可能还是比较混乱 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(一)
系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(二)
系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(三)
系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(四)
系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...
随机推荐
- Nodejs in Visual Studio Code 02.学习Nodejs
1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...
- 深度优先搜索-linux上浅显易懂的例子
上次看啊哈算法中的深度优先搜索,自己用的是linux(linux粉,windows黑,嘿嘿),字符界面,为了强化对这个的理解,就在linux上对这个例子的代码做了一点修改可以很清楚的看到整个搜索过程, ...
- B - I Hate It - hdu 1754
Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟 ...
- F - Truck History - poj 1789
有一个汽车公司有很多年的汽车制造历史,所以他们会有很多的车型,现在有一些历史学者来研究他们的历史,发现他们的汽车编号很有意思都是有7个小写字母组成的,而且这些小写字母具有一些特别的意义,比如说一个汽车 ...
- GC与显式内存管理
C++复兴的话题至今已被鼓吹两年有余,Herb Sutter和Bjarne Stroustrup等大牛们也为C++带来了大步伐的革新.然而,从这两年的效果而言,C++的复兴并没有发生.一方面随着世界经 ...
- Oracle执行计划——使用index full scan的几种情况
常见有三种情况都有用到indexfull scan. 1. 查询列就是索引列 2. 对索引列进行order by时 3. 对索列进行聚合计算时
- lesson4:使用锁Lock来解决重复下单的问题
demo源码:https://github.com/mantuliu/javaAdvance 中的类Lesson4IndependentLock 在上一节中,我们分析了Lock的源代码并一起实践了粗粒 ...
- [AngularJS] New in Angular 1.5 ng-animate-swap
<!DOCTYPE html> <html ng-app="MyApplication"> <head> <link rel=" ...
- Javascript 第一阶段 学习使用总结
JavaScript 是一种轻量级的编程语言.JavaScript 是可插入 HTML 页面的编程代码.脚本可被放置在 HTML 页面的 <body> 和 <head> 部分中 ...
- codevs 1733 聪明的打字员 (Bfs)
/* Bfs+Hash 跑的有点慢 但是codevs上时间限制10s 也ok */ #include<iostream> #include<cstdio> #include&l ...