Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hangfire to perform background tasks in ASP.NET

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people have no idea about how to use Hangfire to perform background tasks in ASP.NET. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Hangfire is an open source and commercially free tool library. It allows you to easily perform multiple types of background tasks in ASP.NET applications (or not in ASP.NET applications) without customizing the development and management of Windows Service-based background task executors. And the task information can be persisted. The built-in console provides integrated services.

You can install Hangfire:Install-Package Hangfire in your application through Nuget

Hangfire has the following features and points:

Support for queue-based task processing: task execution is not synchronous, but is placed in a persistence queue to immediately return request control to the caller. Usage: BackgroundJob.Enqueue (() = > Console.WriteLine ("Simple!"))

Delay task execution: instead of calling the method immediately, set a future time point to execute. Usage: BackgroundJob.Schedule (() = > Console.WriteLine ("Reliable!"), TimeSpan.FromDays (7))

Loop task execution: you can add repetitive tasks with a simple line of code, built-in common time loop patterns, or you can set complex patterns based on CRON expressions. Usage: RecurringJob.AddOrUpdate (()) = > Console.WriteLine ("Transparent!"), Cron.Daily)

Persistent storage of tasks, queues, statistics: SQL Server is used by default, or message queuing can be used to reduce queue processing latency, or Redis can be configured for better performance

Built-in automatic retry mechanism: you can set the number of retries and manually restart tasks in the console

In addition to calling static methods, instance methods are also supported

Ability to capture multilingual state: that is, the caller's Thread.CurrentCulture and Thread.CurrentUICulture information can be persisted with the task so that the multilingual information is consistent when the task is executed

Support for task cancellation: use a mechanism such as CancellationToken to handle task cancellation logic

Support for IoC containers: currently supports open source IoC containers commonly used by Ninject and Autofac

Support for Web clustering: you can run multiple Hangfire instances on one or more machines for redundant backups

Support for multiple queues: multiple queues can be supported for the same Hangfire instance to better control how tasks are executed

Concurrency level control: the default is 5 times the number of processors, of course, you can also set your own

Good scalability: there are many extension points to control persistent storage, IoC container support, etc.

Why use a library like Hangfire? I think the advantages are as follows:

Development is simple: queue execution, delayed execution and repeated execution of tasks can be achieved without additional development on your own.

Easy to deploy: can be deployed with the main ASP.NET application, testing and maintenance are relatively simple

Migration is simple: since the host is not limited to ASP.NET, it is very easy to put the task executor somewhere else in the future (what needs to be changed is to start the Hangfire server in another host)

Simple extension: because it is open source and has many extension points, it can be easily extended when the existing plug-ins do not meet their own needs.

Previously, I applied Hangfire to two situations:

Scientific calculation for a long time in the background: in this way, the request can be returned to the client immediately. After the long calculation is completed in the background, the user can be reminded in real time with SignalR.

Group email in the background: send group mail in batches through services such as SendCloud through deferred and recurring tasks

Of course, there are still many applications for Hangfire, such as processing orders from e-commerce sellers in the background.

The documentation of Hangfire is really amazing. The file of every open source project should be like this. ASP.NET should be well documented as well.

The best feature in Hangfire is that it builds / hangfire dashboards that show you all the preset, ongoing, successful and failed processes. This is a really good add-on.

You can easily queue "fire and forget" jobs and they support persistent queues:

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

You can use delay.

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

Or use large and complex CRON-style circular tasks:

RecurringJob.AddOrUpdate (() = > Console.Write ("Recurring"), Cron.Daily)

After reading the above, have you mastered how to use Hangfire to perform background tasks in ASP.NET? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report