ASP.NET Core开发-后台任务利器Hangfire使用
2016-08-23 08:57
330 查看
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 命令行执行
添加好引用以后我们就可以来使用。
打开Startup.cs
首先在ConfigureServices 方法中注册服务:
这里是配置数据库,数据库需要确保存在,这里配置的是SQL Server数据库,目前官方支持SQL Server。
然后在Configure 方法中加入HangfireServer及HangfireDashboard:
然后选择Kestrel执行,访问地址:http://localhost:5000/hangfire
View Code
通过Hangfire, 这样我们就可以很方便的在ASP.NET Core 里创建后台任务。而且提供管理界面供我们操作。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
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
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(); } 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!"); }); } }
View Code
通过Hangfire, 这样我们就可以很方便的在ASP.NET Core 里创建后台任务。而且提供管理界面供我们操作。
如果你觉得本文对你有帮助,请点击“推荐”,谢谢。
相关文章推荐
- asp.net使用Hangfire开发后台定时任务demo
- Core开发-后台任务利器Hangfire使用
- ASP.NET Core 使用 Hangfire 定时任务
- ASP.NET Core 中使用 Hangfire 定时启动 Scrapyd 爬虫
- Asp.Net Core 集成 Hangfire 配置使用 Redis 存储
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之登录窗口调试
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之调整首页显示
- 使用ASP.NET MVC、Rabbit WeixinSDK和Azure快速开发部署微信后台
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之登录窗口
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(二)-- Web Api Demo
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之完成登录功能
- 使用VS Code开发调试ASP.NET Core 1.0
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(三)-- Logger
- 执行后台任务的利器——Hangfire
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之调整首页显示
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之登录窗口
- 使用 dotnet watch 开发 ASP.NET Core 应用程序
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之完成登录功能
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之调整首页显示
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之登录窗口调试