HangFire overview, hangfire
Overview
Hangfire allows you to call Methods outside the request processing pipeline in a very simple but reliable way. These method calls are executed in an independent background thread, which is called a background task.
The implementation principle of the HangFire library consists of three main components: client, storage, and server. The following is a small figure describing the main processes in Hangfire:
Applicable Platform
Hangfire is not restricted by specific. NET application types. You can use ASP. NET Web applications, non-ASP.NET web applications, console applications, or Windows Services. The following are applicable platforms:
- . NET Framework 4.5
- Persistent storage (listed below)
- Newtonsoft. Json library ≥ 5.0.1
Client
You can use Hangfire to create any types of background tasks: fire-and-forget (release method call), delay (execution after a period of time), and loop (execution by time ).
You do not need to create special classes for Hangfire. Background tasks can be called based on conventional static or instance methods. The following is an instance method call:
var client = new BackgroundJobClient();client.Enqueue(() => Console.WriteLine("Easy!"));client.Delay(() => Console.WriteLine("Reliable!"), TimeSpan.FromDays(1));
There are also simpler ways to create background tasks-BackgroundJob class
You can use static methods to create a method. The following is a static method call:
BackgroundJob.Enqueue(() => Console.WriteLine("Hello!"));
Serialization in HangfireGiven InformationAfter you note and save it in the persistent database, the control is returned to a caller.
Given information: it refers to the parameters passed to HangFire, the method body, and the execution interval.
Task Storage
HangeFire stores background tasks and other information related to background tasks in a persistent library. The persistent library helps background jobs restore the task list after the application restarts.This is usedCLR thread pool and HangfireMain differences between backend tasks. HangFire supports different storage backgrounds:
- SQL Azure, SQL Server 2008 R2 (backward compatible, including Express)
- Redis
SQL Server storage can reduce processing latency through MSMQ or RabbitMQ authorization.
GlobalConfiguration.Configuration.UseSqlServerStorage("db_connection");
Background Service
Background tasks are processed by the Hangfire Server. It is implemented as a set of dedicated (non-thread pool) Background threads, which obtain job tasks from the memory and process them. The server is also responsible for keeping the storage clean and automatically deleting old data.
What you need to do is to createBackgroundJobServer
Class instance and start to handle some things:
using (new BackgroundJobServer()){ Console.WriteLine("Hangfire Server started. Press ENTER to exit..."); Console.ReadLine();}
Hangfire uses reliable extraction algorithms for each storage backend. Therefore, you can start background processing programs in Web applications, the background tasks are not lost when the application is restarted or the process is terminated.
Summary
Previously, we processed background tasks. Generally, we had our own independent Windows Service, which is responsible for running Windows Services, maintaining the running of background tasks, auditing records, and error handling, there are also parameters related to background tasks. Now, with HangFire, you can send all these tasks to HangFire. We only need to process the business and simplify our workload.
Http://docs.hangfire.io/en/latest/index.html.