.NET and use the quick start Hangfire

Preface:

  Timing task scheduling problem is a commonplace problem. There are many online cron job scheduling solution for me a long time ago mainly to use Window Window service programs and the timing of implementation of tasks to do, and then you start a timed task scheduling framework Quartz.Net. But has not started too Hangfire this comes with a background task scheduling panel, task scheduling framework can be magical manually perform tasks in the background. Some time ago he finally started to bite through online access to some information and view the Hangfire demo in Github is finally spend Hangfire in my own projects. In this article the main brief What are the basic features and benefits and are using MySQL Hangfire, Hangfire's, MS SQL Server as the storage use.

First, what is Hangfire:

  Hangfire is an open source .NET framework for task scheduling, provides built-in integrated console, you can intuitively obvious situation to see job scheduling, and Hangfire not need to rely on a separate application execution (such as: windows services, window plan). And support for persistent storage.

Two, Hangfire conditions:

Hangfire nothing to do with a particular type of .NET application. You can use it in an ASP.NET Web application, non ASP.NET Web application, a console application or a Windows service. The following are the requirements:

1.NET Framework 4.5

2. Permanent storage (Hangfire the background job and other information processing related to remain in permanent memory, so it is necessary repository to store, such as: MS SQL Server, Redis, MySQL, PostgreSql, etc.)

3.Newtonsoft.Json library ≥5.0.1

Three, Hangfire basic features and advantages:

Through the official website of the picture you will know how good it is a task scheduling framework, as shown below:

 

 

Four, Hangfire to install and use:

There are about Hangfire of a series of software packages on NuGet:

Details Address:  https://www.nuget.org/packages?q=Hangfire

By entering the install command to install the package management console Hangfire required NuGet package:

Use NuGet MS SQL Server as the storage we need to install:

 Use Hangfire installed in an ASP.NET application: 

Install-Package Hangfire

In the console application or window server processing operations:

Install-Package Hangfire.Core
Install-Package Hangfire.SqlServer

Note that is not recommended in a console application or window server directly install: Install-Package Hangfire, because it's just a quick start package, and you may not include dependencies (eg, Microsoft.Owin.Host.SystemWeb etc. unrelated dependencies).

MySQL as NuGet when we need to install memory:

 Use Hangfire installed in an ASP.NET application: 

 Install-Package Hangfire.Core 

We also need to install a MySql storage (Hangfire.MySqlStorage) expansion, pay attention because Hangfire itself does not support MySQL storage, which is called: Arnoldas Gudas of expansion :

Nuget Address: https://www.nuget.org/packages/Hangfire.MySqlStorage/

Installation command:

Note: Because of my project is .NET Framework, Version = v4.5.1 version, we can only install the 1.0.7 version, we see needs may be

Install-Package Hangfire.MySqlStorage -Version 1.0.7

When we want to use (host) IIS hosting ASP.NET applications, we also need to install:

Install-Package Microsoft.Owin.Host.SystemWeb -Version 4.0.1

 

And adding configure OWIN Startup.cs, and connected to the corresponding store database:

Add OWIN Startup.cs

  Here it is when there is no Startup.cs if your project needs to do anything to add!

What is OWIN Startup.cs:

A brief overview: .NET platform is an open web interface, Startup is .Net and web communication channels, play forward, communication role.

Details description: https://www.cnblogs.com/wj033/p/6065145.html

Attached libraries need to use in Startup.cs in:

        the Configuration void public (IAppBuilder App) 
        { 
            // use SqlServer stored in the corresponding name web.config connectionStrings in 
            GlobalConfiguration.Configuration.UseSqlServerStorage ( "sqlserver_connection"); 
// Note that when you are using MySql is stored as required follows // use MySql stored in the corresponding name web.config connectionStrings in GlobalConfiguration.Configuration.UseStorage (new new MySqlStorage ( "mysql_connection")); app.UseHangfireDashboard (); // arranged background dashboard app.UseHangfireServer () ; // start using the service Hangfire }

Web.config database configuration:

1.MS SQL Server in:

<connectionStrings>
    <add name="sqlserver_connection" connectionString="Data Source=.;Initial Catalog=MyFirstDb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

2.MySQL in:

 <connectionStrings>
    <add name="mysql_connection" providerName="System.Data.MySqlClient" connectionString="Server=123.xxx.xxx.xx;Port=3306;Database=MyFirstDb;Uid=root;
Pwd=youpassword;charset=utf8;SslMode=none;Allow User Variables=True" />
</connectionStrings>

Run the program, visit scheduling control panel:

  When we have completed the above configuration, and the program can normal bug-free operation, our Hangfire Dashboard (dashboard) in our local can be a normal visit (Hangfire dashboard by default only supports local access), if need remote access, then we may need to do the corresponding configuration unauthorized operation!  

Run successfully, view the database whether to generate a list of related Hangfire:

  After a successful first run, you can see Hangfire open the database has been automatically created for us some of the scheduled tasks list of regular tasks, the timing queue, services, and other related state data table (show persistence characteristics Hangfire role), the following figure It shows:

a.MS SQL Server generated table:

 

 b.MySQL generated table:

 

 Access scheduling control panel:

Local access method: https: // localhost: port number / hangfire /

FIG Effect scheduling control panel:

 

 Background scheduling common tasks to create and use:

// support queue-based task processing: task execution is not synchronized, but into a persistent queue to request immediately returns control to the caller. 
var jobId = BackgroundJob.Enqueue (() => WriteLog ( " Queue task " )); 

// delay task execution: instead of calling the method immediately, but set a future point in time come to perform, delay job is only executed once 
var jobId = .schedule BackgroundJob (() => Console .WriteLine ( "" ), the TimeSpan .FromDays ( . 1 )); // perform this task day after 

// cycle tasks are performed: add the line of code repeatedly executed task, its built-common time cycle mode, may be set based on the CRON complex expression pattern. [Comparison of multi-use] 
RecurringJob.AddOrUpdate (() => WriteLog ( " mission every minute " ), Cron.Minutely); // Note the smallest unit is minutes 

// continuity task execution: Similar in .NET task, you can immediately perform another task again after the completion of the first task execution
BackgroundJob.ContinueWith (the jobId, () => WriteLog ( " continuous task " ));

to sum up:

  Through this project practice it did make me feel the charm Hangfire, really can be said to be easy to get started, a task scheduling framework out of the box. And the framework to do the best that official documents in detail, and also provides a complete demo examples. Finally, as the authors point Hangfire praise!

Hangfire related to the use of learning materials:

Official website address: https://www.hangfire.io/

GitHub Source: https://github.com/HangfireIO/Hangfire

Chinese document: https://www.bookstack.cn/read/Hangfire-zh-official/README.md 

GitHub use the sample source code: https://github.com/HangfireIO/Hangfire.Samples (including the console application, window service, ASP.NET MVC, WebForm)

Hangfire use the article summary: https://www.bbsmax.com/R/xl56E0nrJr/

 

Guess you like

Origin www.cnblogs.com/Can-daydayup/p/11610747.html
Recommended