# Dotnetcore Timer service Hangfire recently wrote an ASP. NET Core project, which is useful to timed tasks, and is ready to use quartz.net. After all, both in Java and in the. NET Framework are outstanding. But after looking at Quartz.net's document about ASP. NET core, I decided to switch to a timed task frame, so I chose Hangfire. Development tools: Visual Studio Code Environment:. NET core MVC 2.0① Create an ASP. Open an empty folder in VS code, and then use CTRL +, (or click on the view at the top of the terminal) to open the terminal input: dotnet New Mvc② install Hangfire in Terminal input: dotnet Add package Hangfire③ Open startup file, import dependencies: Using Hangfire; NOTE: This timing framework requires a database, if you use MSSQL Do not separate the package of the database, otherwise need to be added separately, such as dotnet add packages Hangfire.pro.redis;④ Add Database engine in Configureservices "" Publicvoidconfigureservices (iservicecollection services) {services. Addhangfire (R =>r.usesqlserverstorage (@" Server= (LocalDB) \mssqllocaldb; Database=hangfire; trusted\_connection=true;")); Services. Addmvc (); } ' ⑤ then add the app by adding a scheduled task at configure start the service launch framework. Usehangfireserver (); app. Usehangfiredashboard (); Recurringjob.addorupdate ();p ublicvoidconfigure (Iapplicationbuilder app, ihostingenvironment env) {if (E Nv. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } else {app. Useexceptionhandler ("/Home/Error"); } app. Usehangfireserver (); App. Usehangfiredashboard (); Timed Tasks Recurringjob.addorupdate (() =>newcommon.weather (), cron.daily); App. Usestaticfiles (); App. USEMVC (Routes => {routes. MapRoute (name: "default", Template: "{ Controller=home}/{action=index}/{id?} "); }); Note: Common is a tool class for me, weather is the method that I need to execute on a timed basis; The cron.daily table is executed once a day, and there are many ways to execute a scheduled task, such as adding, delaying, repeating, and so on. Cron expressions are also available in a variety of ways: [HTTP/ Docs.hangfire.io/en/latest/quick-start.html] (http://docs.hangfire.io/en/latest/quick-start.html) cron expression: [HTTPS ://en.wikipedia.org/wiki/cron#cron\_expression] (https://en.wikipedia.org/wiki/Cron#CRON_expression) if using vs Code words do not execute dotnet Res before commissioningTore or dotnet Build
Dotnetcore Timer service Hangfire