Hangfire-執行定時任務框架
1、新建netframwork console 控制台項目ConsoleHangfireTest,nuget程序包 添加Hangfire
2、項目ConsoleHangfireTest右鍵,添加OWIN Startup 類
代碼內容:
using System; using System.Threading.Tasks; using System.Web.Http; using Hangfire; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(ConsoleHangfireTest.Startup1))] namespace ConsoleHangfireTest { public class Startup1 { public void Configuration(IAppBuilder app) { //HttpConfiguration configuration = new HttpConfiguration(); //configuration.Routes.MapHttpRoute( // name: "default", // routeTemplate: "api/{controller}/{action}/{id}", // defaults: new { id = RouteParameter.Optional } // ); //app.UseWebApi(configuration); // 有關如何配置應用程序的詳細信息,請訪問 https://go.microsoft.com/fwlink/?LinkID=316888 //定時任務的持久化使用Sqlserver進行,當然也可以使用mongodb,redis等其他的 GlobalConfiguration.Configuration.UseSqlServerStorage("Data Source=MOP6EXV9E5J1M1F;Initial Catalog=mydb;Integrated Security=True;"); //啟用Hangfire服務 app.UseHangfireServer(); //啟用Hangfire Dashboard面板 app.UseHangfireDashboard(); } } }
3、項目ConsoleHangfireTest右鍵,添加TestHangfire
代碼如下:
using Hangfire; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleHangfireTest { public static class TestHangfire { [Obsolete] public static void Start() { //支持基於隊列的任務處理:任務執行不是同步的,而是放到一個持久化隊列中,以便馬上把請求控制權返回給調用者。 var jobId = BackgroundJob.Enqueue(() => WriteLog("------Enqueue 隊列任務")); //延遲任務執行:不是馬上調用方法,而是設定一個未來時間點再來執行。 BackgroundJob.Schedule(() => WriteLog("------Schedule 延時任務"), TimeSpan.FromSeconds(10)); //循環任務執行:一行代碼添加重復執行的任務,其內置了常見的時間循環模式,也可基於CRON表達式來設定復雜的模式。 RecurringJob.AddOrUpdate(() => WriteLog("------AddOrUpdate 每分鍾執行任務"), Cron.Minutely); //注意最小單位是分鍾 //延續性任務執行:類似於.NET中的Task,可以在第一個任務執行完之后緊接着再次執行另外的任務 BackgroundJob.ContinueWith(jobId, () => WriteLog("------ContinueWith 連續任務")); } public static void WriteLog(string msg) { Trace.WriteLine($"Hangfire在 {DateTime.Now} 執行了任務:[{msg}]"); } } }
4、項目ConsoleHangfireTest的Program.cs內容如下:
using Hangfire; using Microsoft.Owin.Hosting; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleHangfireTest { class Program { static void Main(string[] args) { Console.WriteLine("啟動服務。。。"); WebApp.Start<Startup1>("http://localhost:8090/");//注意之后的斜杠不要忘記了 //string baseAddress = "http://127.0.0.1:8090/"; //WebApp.Start<Startup>(url: baseAddress); Console.WriteLine("服務啟動成功。。。"); //TestHangfire.Start(); Start(); Console.ReadLine(); } [Obsolete] public static void Start() { //支持基於隊列的任務處理:任務執行不是同步的,而是放到一個持久化隊列中,以便馬上把請求控制權返回給調用者。 var jobId = BackgroundJob.Enqueue(() => WriteLog("Enqueue 隊列任務")); //延遲任務執行:不是馬上調用方法,而是設定一個未來時間點再來執行。 BackgroundJob.Schedule(() => WriteLog("Schedule 延時任務"), TimeSpan.FromSeconds(10)); //循環任務執行:一行代碼添加重復執行的任務,其內置了常見的時間循環模式,也可基於CRON表達式來設定復雜的模式。 RecurringJob.AddOrUpdate(() => WriteLog("AddOrUpdate 每分鍾執行任務"), Cron.Minutely); //注意最小單位是分鍾 //延續性任務執行:類似於.NET中的Task,可以在第一個任務執行完之后緊接着再次執行另外的任務 BackgroundJob.ContinueWith(jobId, () => WriteLog("ContinueWith 連續任務")); } public static void WriteLog(string msg) { Debug.WriteLine($"Hangfire在 {DateTime.Now} 執行了任務:[{msg}]"); } } }
5、運行如下:
瀏覽器地址欄輸入:http://localhost:8090/hangfire/
Hangfire的儀表盤顯示
數據庫顯示:
注意:
如果運行時報錯誤,“ System.MissingMemberException:找不到服務器工廠”
System.MissingMemberException: The server factory could not be located for the given input: Microsoft.Owin.Host.HttpListener
其實是缺少Microsoft.Owin.Host.HttpListener,使用NuGet添加一下即可