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());
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();
} 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());
return context.Response.WriteAsync("ok");
});
}); app.Run(context =>
{
return context.Response.WriteAsync("Hello from ASP.NET Core!");
});
}
}

通过Hangfire, 这样我们就可以很方便的在ASP.NET Core 里创建后台任务。而且提供管理界面供我们操作。

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。

ASP.NET Core开发-后台任务利器Hangfire使用的更多相关文章

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

    Core开发-后台任务利器Hangfire使用 ASP.NET Core开发系列之后台任务利器Hangfire 使用. Hangfire 是一款强大的.NET开源后台任务利器,无需Windows服务/ ...

  2. Visual Studio 2017 ASP.NET Core开发

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

  3. [转]ASP.NET Core 开发-Logging 使用NLog 写日志文件

    本文转自:http://www.cnblogs.com/Leo_wl/p/5561812.html ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 . ...

  4. ASP.NET Core 开发-中间件(Middleware)

    ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 每个组件选择是否传递给管道中的下一个组件的请求,并能之前和下一组 ...

  5. ASP.NET Core开发-Docker部署运行

    ASP.NET Core开发Docker部署,.NET Core支持Docker 部署运行.我们将ASP.NET Core 部署在Docker 上运行. 大家可能都见识过Docker ,今天我们就详细 ...

  6. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  7. ASP.NET Core 开发-Entity Framework (EF) Core 1.0 Database First

    ASP.NET Core 开发-Entity Framework Core 1.0 Database First,ASP.NET Core 1.0 EF Core操作数据库. Entity Frame ...

  8. ASP.NET Core 开发-Logging 使用NLog 写日志文件

    ASP.NET Core 开发-Logging 使用NLog 写日志文件. NLog 可以适用于 .NET Core 和 ASP.NET Core . ASP.NET Core已经内置了日志支持,可以 ...

  9. ASP.NET Core 开发-中间件(StaticFiles)使用

    ASP.NET Core 开发,中间件(StaticFiles)的使用,我们开发一款简易的静态文件服务器. 告别需要使用文件,又需要安装一个web服务器.现在随时随地打开程序即可使用,跨平台,方便快捷 ...

随机推荐

  1. 玩转Windows服务系列——创建Windows服务

    创建Windows服务的项目 新建项目->C++语言->ATL->ATL项目->服务(EXE) 这样就创建了一个Windows服务项目. 生成的解决方案包含两个项目:Servi ...

  2. C#设计模式之职责链

    Iron之职责链 需求: "Iron"的建造一直没有停止,现在单个部件是有的,但是在部件从工厂里出来的时候,在组装到一起之前,我们还是非常有必要对部件进行质量检测,或者是其它个方面 ...

  3. Oracle没有WM_CONCAT函数的解决办法

    WM_CONCAT是oracle的非公开函数,并不鼓励使用,新版本oracle并没有带此函数,需要手工加上. 1.下载三个文件:owmctab.plb  . owmaggrs.plb . owmagg ...

  4. C语言 · 求矩阵各个元素的和

    问题描述 这里写问题描述. 输入格式 测试数据的输入一定会满足的格式. 例:输入的第一行包含两个整数n, m,分别表示矩阵的行数和列数.接下来n行,每行m个正整数,表示输入的矩阵. 输出格式 要求用户 ...

  5. MySQL数据库数据存放位置修改

    MySQL数据库数据存放位置修改 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方 ...

  6. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

  7. Atitit  补充说明 sql知识图谱与线路图attilax总结补充说明

    Atitit  补充说明 sql知识图谱与线路图attilax总结补充说明 1. 常见编程语言的分类  :命令式语言.函数式语言.逻辑语言1 1.1. 按照编程语言的代际划分,又2gl,3gl,4gl ...

  8. CI Weekly #3 | 关于微服务、Docker 实践与 DevOps 指南

    CI Weekly 围绕『 软件工程效率提升』 进行一系列技术内容分享,包括国内外持续集成.持续交付,持续部署.自动化测试. DevOps 等实践教程.工具与资源,以及一些工程师文化相关的程序员 Ti ...

  9. Report processing of Microsoft Dynamic AX

    Report processing of Microsoft Dynamic AX 版权声明:本文为博主原创文章,未经博主允许不得转载. The implementation of a general ...

  10. XE1:使用SSMS创建Extended Events

    Extended Events 用于取代SQL trace,是SQL Server 追踪系统运行的神器,其创建过程十分简单. 一,创建Extended Events的Session step1,打开N ...