This article is a mirror article of machine translation, please click here to jump to the original article.

View: 61378|Reply: 6

[ASP.NET] asp.net background task hangfire framework tutorial

[Copy link]
Posted on 5/24/2019 9:51:10 AM | | | |
Hangfire works with most .NET platforms:.NET Framework 4.5 or later, .NET Core 1.0 or later, anything related to . NET Standard 1.3 compatible platform. You can integrate it with almost any application framework, including ASP.NET, ASP.NET Core, console applications, Windows services, WCF, and community drivers like Nancy or ServiceStack.

Applicable scenarios:The background sends the email asynchronously and returns a response to the user as soon as possible. Scheduled execution of tasks, cyclical execution of tasks, asynchronous execution of time-consuming tasks, completion of task A execution and then execution of B tasks, and batch execution of tasks.
How I used to do it:

c# encapsulates a universal QueueHelper queue
https://www.itsvse.com/thread-5029-1-1.html
(Source: Architect_Programmer)


You can put background processing in ASP.NET application without using other processes like Windows services. Hangfire's code is ready for deploymentUnexpected process termination, application pool recycling, and restart。 Due to the usePersistent storage, hence youNo background jobs are lost

First, the renderings are as follows:




The picture above is the Hangfire Dashboard dashboard, which is very useful and can check the status of background tasks, and it is also very cool, so it must be configured.

The dashboard UI requires a startup class, only if youWhen configuring Hangfire with the Startup class,Dashboard UIIt can be used

At first, I didn't configure the Hangfire Dashboard dashboard and got the following error:

The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- No assembly found containing a Startup or [AssemblyName]. Startup class.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.



Solution:

When using Global.asax.cs-based initialization, you may also need to disable OWIN's startup class detection.

We start to get to the point, the tutorial begins:

1: The framework should meet the needs of hangfire,My project is .net 4.6.2, So, you can install hangfire normally, .net 4.0 projects don't need to try, not supported!

2: The nuget command is as follows:

3: The OWIN Startup class is designed to keep the web application bootstrapping logic in one place. In Visual Studio 2013, you can add it by right-clicking on the project and selecting the Add/OWIN Startup Class menu item. As shown below:



Create a new Startup.cs file in the project root directory.

4: Configurationdatabase connection string, the SQL Server 2012 version I am using here, the configuration in web.config is as follows:

5: Configure the Hangfire Dashboard to access the filter, create a new oneHangfireAuthorizationFilter.csFile.

By default, only local access to the Hangfire Dashboard is allowed. Dashboard authorization must be configured to allow remote access.


6: Configure the Startup.cs class, the code is as follows:


7: Start a website project

Run the application F5 by pressing debug mode (this requires looking at the output of the Debug.WriteLine method). Then check the following message in the Output window to see if the background processing has started successfully.



At the same time, the database also has 11 more tables to ensure that all tasks can be executed normally, and the tasks will not be lost under IIS reclamation or process restart, as shown in the figure below:



http://<your-web-app>/hangfire

Visit the Dashboard UI URL to see the rendering at the top of the article.

Attached usage

Fire-And-forget (publish/subscribe)

This is the main type of background task that the persistent message queue handles. When you create a publish/subscribe task, the task will be saved to the default queue (the default queue is "Default", but multiple queues are supported). Multiple dedicated workers listen to this queue and fetch tasks from it and complete tasks.


Delay

If you want to delay the execution of certain tasks, you can use the following tasks. After a given delay time, the task is queued and executed like a publish/subscribe task.

Loop

To call the method by periodicity (hours, days, etc.), use the RecurringJob class. In complex scenarios, you can use CRON expressions to specify a scheduled time to process tasks.


Continuous

Continuity allows you to define complex workflows by linking multiple background tasks together.


Official documentation link:The hyperlink login is visible.

(End)






Previous:Video data algorithm shares notes
Next:Request aborted: Failed to create an SSL/TLS secure channel solution
Posted on 5/24/2019 10:12:25 AM |
This post was last edited by YuAn on 2019-5-24 10:20

Nice article learned
Posted on 5/24/2019 10:12:46 AM |
Support Support
Posted on 9/17/2021 9:59:44 PM |

Support Support
Posted on 9/26/2022 10:20:45 PM |
Support, learn how to use it
Posted on 11/12/2022 7:02:23 PM |

Support, learn how to use it
Posted on 3/25/2023 9:45:31 AM |

Support, learn how to use it
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com