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使用的更多相关文章

  1. ASP.NET Core开发-后台任务利器Hangfire使用

    ASP.NET Core开发系列之后台任务利器Hangfire 使用. Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/任务计划程序. 可以使用于ASP.NET 应用也 ...

  2. Visual Studio 2017 ASP.NET Core开发

    Visual Studio 2017 ASP.NET Core开发,Visual Studio 2017 已经内置ASP.NET Core 开发工具. 在选择.NET Core 功能安装以后就可以进行 ...

  3. NET Core开发

    NET Core开发 Visual Studio 2017 ASP.NET Core开发,Visual Studio 2017 已经内置ASP.NET Core 开发工具. 在选择.NET Core ...

  4. ASP.NET Core-组件-后台任务:Hangfire

    ylbtech-ASP.NET Core-组件-后台任务:Hangfire Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的 ...

  5. 基于 abp vNext 和 .NET Core 开发博客项目 - 完善与美化,Swagger登场

    上一篇文章(https://www.cnblogs.com/meowv/p/12896898.html)已经成功将博客项目跑起来了,那么本篇主要是将之前遗留的问题解决,现在的代码看起来可能还是比较混乱 ...

  6. 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(一)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  7. 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(二)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  8. 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(三)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

  9. 基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(四)

    系列文章 基于 abp vNext 和 .NET Core 开发博客项目 - 使用 abp cli 搭建项目 基于 abp vNext 和 .NET Core 开发博客项目 - 给项目瘦身,让它跑起来 ...

随机推荐

  1. SQL:将查询结果插入到另一个表的三种情况

    一:如果要插入目标表不存在: select * into 目标表 from 表 where ... 二:如果要插入目标表已经存在: insert into 目的表 select * from 表 wh ...

  2. windows server 2003 64x 读取office数据终极解决办法 The 'Microsoft.Jet.OLEDB.4.0' provider is not registered

    微软老子信了你的邪!      试了各种办法没有效果 网友解决办法一: The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the ...

  3. python:文本文件处理

    # coding=utf-8 #将列表写入文件 :'w+'(覆盖原有文件内容),'a+'(在原文件的基础上追加) def write_list_test(path,savelist,pattarn): ...

  4. Managing Hierarchical Data in MySQL

    Managing Hierarchical Data in MySQL Introduction Most users at one time or another have dealt with h ...

  5. web前端面试试题总结---css篇

    CSS 介绍一下标准的CSS的盒子模型?低版本IE的盒子模型有什么不同的? (1)有两种, IE 盒子模型.W3C 盒子模型: (2)盒模型: 内容(content).填充(padding).边界(m ...

  6. Java实现BASE64编解码

    Java实现BASE64编解码 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...

  7. Ubuntu12.04下使用valgrind内存测试工具测试Qt程序

    1. 到官网http://valgrind.org/downloads/上下载valgrind最新版本: 2. 解压源码,执行./configure;make;make install后,默认安装到/ ...

  8. Java基础知识强化84:System类之exit()方法和currentTimeMillis()方法

    1. exit方法: public static void exit(int status): 终止当前正在运行的Java虚拟机.参数用作状态码:根据惯例,非0的状态码表示异常终止. 调用System ...

  9. mysql myisam 锁表问题<转>

    转自http://yafei001.iteye.com/blog/1841258 锁是计算机协调多个进程或线程并发访问某一资源的机制 .在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用 ...

  10. 判断两条直线的位置关系 POJ 1269 Intersecting Lines

    两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, ...