The Net Core development series is followed by a task tool hangfire use.
The Hangfire is a powerful one. NET open source Background task tool without Windows Service/Task Scheduler.
Can be used with an ASP. NET application or it can be used in the console. Hangfire can create new, different kinds of tasks with just a few simple lines of code.
Currently, Hangfire has supported. NET core, and now we're going to show you how to use it in ASP.
Hangfire Github:https://github.com/hangfireio/hangfire
Official website: http://hangfire.io/
Related documents: http://docs.hangfire.io/en/latest/
First we create a new ASP. NET Core Web application
Select template-"Empty template"
Then add the reference:
NuGet command line execution
Install-package Hangfire
Add a good reference and we can use it later.
Open Startup.cs
First, register the service in the Configureservices method:
Public void configureservices (iservicecollection services) { services. Addhangfire (R=>r.usesqlserverstorage ("Data source=.;i Nitial Catalog=hangfiredemo; User Id=sa; password=123456")); }
Here is the configuration database, the database needs to be ensured, the SQL Server database is configured here, and the SQL Server is officially supported.
Then add Hangfireserver and Hangfiredashboard to the Configure method:
Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {Logger Factory.addconsole (); if(env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } App. Usehangfireserver (); app. Usehangfiredashboard (); app. Run (Context= { returnContext. Response.writeasync ("Hello from ASP. core!"); }); }
Then select Kestrel Execution, access address: Http://localhost:5000/hangfire
Run successfully, we can add the task below.
App. Map ("/index", r ={r.run (context= { //The task executes once per minuteRecurringjob.addorupdate (() = Console.WriteLine ($"ASP. NET Core Linezero"), cron.minutely ()); returnContext. Response.writeasync ("OK"); }); }); App. Map ("/one", r ={r.run (context= { //Task Execution OnceBackgroundjob.enqueue (() = Console.WriteLine ($"ASP. NET Core one Start Linezero{datetime.now}")); returnContext. Response.writeasync ("OK"); }); }); App. Map ("/await", r ={r.run (context= { //task delay two minutes executionBackgroundjob.schedule (() = Console.WriteLine ($"ASP. NET Core await Linezero{datetime.now}"), Timespan.fromminutes (2)); returnContext. Response.writeasync ("OK"); }); });
The task is created for convenience only, we can also create it at initialization time, or it can be created in the controller.
Let's do that.
First visit http://localhost:5000/index
Then visit http://localhost:5000/await
Last Visit Http://localhost:5000/one
So the task will be carried out.
Jobs is to see all the tasks, we can operate in the program interface operation and deletion, very convenient.
We can also click on a task to see the details of the task. And the results of the task execution.
Finally run for a while, and the chart shows
Startup.cs Complete code:
1 Public classStartup2 {3 //This method gets called by the runtime. Use this method to add services to the container.4 //For more information on how to configure your application, visithttp://go.microsoft.com/fwlink/?LinkID=3989405 Public voidconfigureservices (iservicecollection services)6 {7Services. Addhangfire (R=>r.usesqlserverstorage ("Data Source=.;i Nitial Catalog=hangfiredemo; User Id=sa; password=123456"));8 }9 Ten //This method gets called by the runtime. Use this method to configure the HTTP request pipeline. One Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) A { - loggerfactory.addconsole (); - the if(env. Isdevelopment ()) - { - app. Usedeveloperexceptionpage (); - } + //GlobalConfiguration.Configuration.UseSqlServerStorage ("Data source=.;i Nitial Catalog=orchardcms; User Id=sa; password=sa123456 "); - + app. Usehangfireserver (); A app. Usehangfiredashboard (); at -App. Map ("/index", r = - { -R.run (context = - { - //The task executes once per minute inRecurringjob.addorupdate (() = Console.WriteLine ($"ASP. NET Core Linezero"), cron.minutely ()); - returnContext. Response.writeasync ("OK"); to }); + }); - theApp. Map ("/one", r = * { $R.run (context =Panax Notoginseng { - //Task Execution Once theBackgroundjob.enqueue (() = Console.WriteLine ($"ASP. NET Core one Start Linezero{datetime.now}")); + returnContext. Response.writeasync ("OK"); A }); the }); + -App. Map ("/await", r = $ { $R.run (context = - { - //task delay two minutes execution theBackgroundjob.schedule (() = Console.WriteLine ($"ASP. NET Core await Linezero{datetime.now}"), Timespan.fromminutes (2)); - returnContext. Response.writeasync ("OK");Wuyi }); the }); - Wu -App. Run (context = About { $ returnContext. Response.writeasync ("Hello from ASP. core!"); - }); - } -}
View Code
With Hangfire, we can easily create a background task in ASP. and provide a management interface for us to operate.
If you think this article is helpful to you, please click " recommend ", thank you.
ASP. NET core Development-Background task tool Hangfire use