Installation

There are a couple of packages for Hangfire available on NuGet. To install Hangfire into your ASP.NET application withSQL Server storage, type the following command into the Package Manager Console window:

PM> Install-Package Hangfire

Configuration

After installing the package, add or update the OWIN Startup class with the following lines:

using Hangfire;

// ...

public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("<connection string or its name>"); app.UseHangfireDashboard();
app.UseHangfireServer();
}

Authorization configuration required

By default only local access is permitted to the Hangfire Dashboard. Dashboard authorization must be configured in order to allow remote access.

Then open the Hangfire Dashboard to test your configuration. Please, build the project and open the following URL in a browser:

http://<your-site>/hangfire

Usage

Add a job…

Hangfire handles different types of background jobs, and all of them are invoked on a separate execution context.

Fire-and-forget

This is the main background job type, persistent message queues are used to handle it. Once you create a fire-and-forget job, it is saved to its queue ("default" by default, but multiple queues supported). The queue is listened by a couple of dedicated workers that fetch a job and perform it.

BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget"));

Delayed

If you want to delay the method invocation for a certain type, call the following method. After the given delay the job will be put to its queue and invoked as a regular fire-and-forget job.

BackgroundJob.Schedule(() => Console.WriteLine("Delayed"), TimeSpan.FromDays(1));

Recurring

To call a method on a recurrent basis (hourly, daily, etc), use the RecurringJob class. You are able to specify the schedule using CRON expressions to handle more complex scenarios.

RecurringJob.AddOrUpdate(() => Console.WriteLine("Daily Job"), Cron.Daily);

Continuations

Continuations allow you to define complex workflows by chaining multiple background jobs together.

var id = BackgroundJob.Enqueue(() => Console.WriteLine("Hello, "));
BackgroundJob.ContinueWith(id, () => Console.WriteLine("world!")); http://docs.hangfire.io/en/latest/quick-start.html#installation

后台任务hangfire的更多相关文章

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

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

  2. 执行后台任务的利器——Hangfire

    今年1月31日,在微软的MVP 2015社区大讲堂上,我给大家分享了一个演讲:在ASP.NET应用中执行后台任务.其中介绍了三种技术的应用:QueueBackgroundWorkItem.Hangfi ...

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

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

  4. .NET Core开源组件:后台任务利器之Hangfire

    一.简述 Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的DI管理着生命周期,免去了在NF4.X环境中配置always ru ...

  5. mvc中使用Hangfire处理后台任务

    考虑下如下代码,在数据保存后,需要发送邮件,发送邮件是个耗时的工作. 我们的目的是,数据保存成功后,就可以返回响应了,发送邮件不重要,不需要等待邮件发送成功 [HttpPost] public Act ...

  6. 执行后台任务的利器——Hangfire

    Hangfire是一个开源且商业免费使用的工具函数库.可以让你非常容易地在ASP.NET应用(也可以不在ASP.NET应用)中执行多种类型的后台任务,而无需自行定制开发和管理基于Windows Ser ...

  7. 后台任务利器之Hangfire

    后台任务利器之Hangfire 一.简述 Hangfire作为一款高人气且容易上手的分布式后台执行服务,支持多种数据库.在.net core的环境中,由Core自带的DI管理着生命周期,免去了在NF4 ...

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

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

  9. .NET Core开源组件:后台任务利器之Hangfire 转载 https://www.cnblogs.com/chenug/p/6655636.html

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

随机推荐

  1. 步步为营-36-ADO.Net简介

    与数据库进行连接交互 方法一 #region 01连接对象 //01 连接字符串 string connstr = "server=.;uid=sa;pwd=sa;database=Demo ...

  2. Oracle学习笔记--第2章 oracle 数据库体系结构

    第2章 oracle 数据库体系结构 目录: ————————————— 2.1物理存储结构 2.1.1数据文件 2.2.2控制文件 2.1.3重做日志文件 2.1.4其他文件 2.2逻辑存储结构 2 ...

  3. [转] 由Request Method:OPTIONS初窥CORS

    刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS…… 目前的工作中,HEAD.PUT.DELE ...

  4. python全栈开发day50-jquery之ajax、XmlHttpRquest

    一.昨日内容回顾 1.jquery位置信息 width() ..,innetWidth() .outWidth() offset().top left scrollTop 2.事件流 DOM2级 (1 ...

  5. BZOJ1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1607 题意概括 给出n个数,每一个数字<1000000,对于每一个数,让你求剩余的n-1个数 ...

  6. Star HDU1541

    看到图和坐标就想到了用二维数组来写   但是连数组的开不下   写好了也爆内存 没看到题目是按照y坐标升序给的坐标 这样就可以忽略y坐标 进行一维的树状数组 #include<bits/stdc ...

  7. ubuntu server 18.04的安装 以及配置网络还有ssh服务

    ubuntu server 18.04的安装 以及配置网络还有ssh服务   服务器是 dell T420 安装过程中规中矩,其中最关键的是分区部分,由于是服务器,如果磁盘比较大的话,一定要用 uef ...

  8. Autodesk系列软件下载

    摘要: 写在前面:下载后如有需要压缩密码的请先使用压缩软件(如:2345好压)打开压缩包,在压缩包的注释或者文本信息中会给出压缩密码!如若没有请私信! 1.3ds Max软件(64位) Autodes ...

  9. [BZOJ2877][NOI2012]魔幻棋盘(二维线段树)

    https://blog.sengxian.com/solutions/bzoj-2877 注意二维线段树的upd()也是一个O(log n)的函数(pushdown()应该也是但没写过). #inc ...

  10. Gym - 100548G The Problem to Slow Down You

    依然是回文树. 我们只需要吧siz[]改成统计两边的siz[][0/1],然后把两个字符中间随便加一个不会出现的字符拼起来,做一遍回文树统计一下就OJBK了 #include<bits/stdc ...