HangFire overview, hangfire

Source: Internet
Author: User

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 classYou 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 createBackgroundJobServerClass 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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.