{"id":2439,"date":"2021-10-07T03:48:03","date_gmt":"2021-10-06T19:48:03","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=2439"},"modified":"2022-02-16T17:24:17","modified_gmt":"2022-02-16T09:24:17","slug":"hangfire","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2021\/10\/07\/hangfire\/","title":{"rendered":"D-08-\u6392\u7a0b\u8a2d\u5b9a ? hangfire"},"content":{"rendered":"<h1>\u5982\u4f55\u8655\u7406\u5b9a\u671f\u7684\u9700\u6c42<\/h1>\n<p>\u76f8\u4fe1\u5f88\u591a\u4eba\u6703\u9047\u5230\u9700\u8981\u5b9a\u671f\u505a\u67d0\u4e9b\u4e8b\u60c5\u7684\u72c0\u6cc1\uff0c\u4f8b\u5982\u6bcf\u5206\u9418\u53bb\u8a08\u7b97\u4e00\u6b21\u8cc7\u6599\uff0c\u6216\u8005\u4e00\u5206\u9418\u8ddf\u8cc7\u6599\u5eab\u6488\u53d6\u8cc7\u6599\u7684\u9700\u6c42\uff0c\u9019\u6642\u5982\u679c\u5bebWindows Service\u6216\u662fConsole\u7a0b\u5f0f\u518d\u642d\u914dWindows\u6392\u7a0b\u4f86\u8a2d\u5b9a\u662f\u4e00\u500b\u89e3\u6c7a\u65b9\u6cd5\uff0c\u4f46\u662f\u6c92\u8fa6\u6cd5\u770b\u5230\u6392\u7a0b\u7684\u76f8\u95dc\u8cc7\u8a0a\uff0c\u800c\u4e14\u9084\u8981\u8a18\u5f97\u53bb\u4f5c\u696d\u7cfb\u7d71\u8a2d\u5b9a\u670d\u52d9\u6216\u662f\u6392\u7a0b\u4e4b\u985e\u7684\uff0c\u4e0d\u904e\u4eca\u5929\u53ef\u4ee5\u6709\u53e6\u5916\u4e00\u500b\u9078\u64c7\uff0c\u8acb\u5927\u5bb6\u7e7c\u7e8c\u770b\u4e0b\u53bb\u3002<\/p>\n<p><!--more--><\/p>\n<h2>hangfire<\/h2>\n<p>\u300c\u524d\u8f29\uff0c\u4eca\u5929\u9700\u8981\u505a\u4e00\u500b\u6bcf\u5206\u9418\u722c\u53d6\u532f\u7387\u7684\u529f\u80fd\u8036\uff0c\u6211\u6c92\u6709\u505a\u904e\u9019\u985e\u7684\u300d<br \/>\n\u4eca\u5929\u5927\u982d\u8981\u4f86\u505a\u4e00\u500b\u4e00\u5206\u9418\u722c\u53d6\u532f\u7387\u7684\u529f\u80fd\uff0c\u4f46\u662f\u56e0\u70ba\u6c92\u6709\u505a\u904e\u6240\u4ee5\u5f88\u82e6\u60f1\uff0c\u9019\u6642\u770b\u5230\u8001K\u8d70\u9032\u4f86\u6240\u4ee5\u6025\u6025\u5fd9\u5fd9\u5730\u8a62\u554f\u4e00\u4e0b\u3002<br \/>\n\u300c\u6069~~~\u4f60\u5c31\u505a\u4e00\u500bwhile\u7136\u5f8cTask.Delay(60000)\u4e0d\u5c31\u597d\u4e86\u3002\u300d<br \/>\n\u8001K\u807d\u5230\u5927\u982d\u7684\u554f\u984c\u5f8c\u5c31\u9019\u6a23\u56de\u7b54\u4ed6\uff0c\u4e0d\u904e\u5927\u982d\u807d\u4e86\u4e4b\u5f8c\u7684\u8868\u60c5\u4e0d\u662f\u5f88\u6eff\u610f\uff0c\u9019\u6642\u5019\u8001K\u7b11\u7b11\u5730\u5c0d\u4ed6\u8aaa\u3002<br \/>\n\u300c\u54c8\u54c8\u54c8\uff0c\u958b\u73a9\u7b11\u7684\u5566\u4f60\u53ef\u4ee5\u5beb\u4e00\u500b\u5beb\u4e00\u500bConsole\u7a0b\u5f0f\uff0c\u7136\u5f8c\u770b\u662f\u639bCronJob\u9084\u662fWindows\u6392\u7a0b\u5c31\u597d\u4e86\u3002\u300d<br \/>\n\u5927\u982d\u807d\u4e86\u9019\u500b\u56de\u7b54\u4e4b\u5f8c\uff0c\u96d6\u7136\u4e0d\u662f\u5f88\u6eff\u610f\u4f46\u662f\u770b\u8d77\u4f86\u6e96\u5099\u8981\u53bb\u958b\u59cb\u52d5\u5de5\u4e86\uff0c\u9019\u6642\u8001K\u53eb\u4f4f\u4e86\u4ed6\u3002<br \/>\n\u300c\u597d\u4e86\u597d\u4e86\u4e0d\u9b27\u4f60\u4e86\uff0c\u525b\u525b\u90fd\u662f\u958b\u73a9\u7b11\u7684\uff0c\u4f60\u53ef\u4ee5\u53bb\u7814\u7a76\u4e00\u4e0bhangfire\u3002\u300d<br \/>\n\u9019\u6642\u5019\u5c0f\u5149\u525b\u597d\u8d70\u4e86\u904e\u4f86\uff0c\u8001K\u770b\u5230\u4ed6\u5f8c\u8ddf\u5927\u982d\u9019\u9ebc\u8aaa\u3002<br \/>\n\u300c\u4f60\u5c31\u8ddf\u4ed6\u4e00\u8d77\u53bb\u7814\u7a76\u4e00\u4e0b\u5427\u3002\u300d<\/p>\n<h3>\u74b0\u5883\u8a2d\u5b9a<\/h3>\n<p>\u5728\u958b\u59cb\u958b\u767c\u6392\u7a0b\u7a0b\u5f0f\u4e4b\u524d\u5148\u8a2d\u5b9a\u4e00\u4e0b\u74b0\u5883\u3002\u6240\u4ee5\u5148\u8f38\u5165\u4ee5\u4e0b\u6307\u4ee4\u4f86\u52a0\u8f09\u5957\u4ef6\u3002<\/p>\n<pre><code class=\"language-bash\">dotnet add package Hangfire.AspNetCore\ndotnet add package Hangfire.Autofac\ndotnet add package Hangfire.MemoryStorage<\/code><\/pre>\n<p>\u9019\u88e1\u8aaa\u660e\u4e00\u4e0b\uff0c\u56e0\u70bahangfire\u6703\u628a\u6392\u7a0b\u4ee5\u53ca\u8981\u8f38\u5165\u7684\u53c3\u6578\u5b58\u653e\u8d77\u4f86\uff0c\u4e0d\u904e\u9019\u908a\u6211\u5011\u53ef\u4ee5\u9078\u64c7\u628a\u8cc7\u6599\u653e\u5728\u8a18\u61b6\u9ad4\u4e2d\uff0c\u5982\u6b64\u53ef\u4ee5\u907f\u514d\u5927\u91cf\u5b58\u53d6\u8cc7\u6599\u5eab\u7684\u554f\u984c\uff0c\u4e0d\u904e\u8981\u5c0f\u5fc3\u8a18\u61b6\u9ad4\u589e\u9577\u7684\u554f\u984c\uff0c\u6240\u4ee5\u8981\u53bb\u6e05\u7406\u5df2\u7d93\u5b8c\u6210\u7684\u8cc7\u6599\u3002\u5728\u52a0\u8f09\u5957\u4ef6\u5b8c\u6210\u4e4b\u5f8c\u6211\u5011\u4f86\u8a3b\u518a\u6392\u7a0b\u5230\u6211\u5011\u7684\u7a0b\u5f0f\u88e1\u9762\uff0c\u9996\u5148\u5148\u5728<code>Statrup.Configure<\/code>\u52a0\u5165\u4ee5\u4e0b\u5167\u5bb9\u3002<\/p>\n<pre><code class=\"language-cs\">GlobalConfiguration.Configuration\n    .UseActivator(new HangfireActivator(provider));\n\napp.UseHangfireServer(new BackgroundJobServerOptions\n{\n    SchedulePollingInterval = TimeSpan.FromMilliseconds(1000)\n});\napp.UseHangfireDashboard(&quot;\/hangfire&quot;,\n    new DashboardOptions()\n    {\n        Authorization = new[] { new AllowAllConnectionsFilter() },\n        IgnoreAntiforgeryToken = true\n    });<\/code><\/pre>\n<p>\u5176\u4e2d<code>SchedulePollingInterval<\/code>\u662f\u8a2d\u5b9a\u591a\u4e45\u6aa2\u67e5\u6392\u7a0b\u4e00\u6b21\uff0c\u9810\u8a2d\u662f15\u79d2\uff0c\u9019\u90e8\u5206\u76f8\u95dc\u5167\u5bb9\u53ef\u4ee5\u53c3\u8003<a href=\"https:\/\/dotblogs.com.tw\/yc421206\/2019\/12\/25\/desktop_app_async_task_via_hangfire\" title=\"\u4f7f\u7528 Hangfire OWIN \u5efa\u7acb\u975e\u540c\u6b65\u4efb\u52d9\" rel=\"nofollow noopener\" target=\"_blank\">\u4f7f\u7528 Hangfire OWIN \u5efa\u7acb\u975e\u540c\u6b65\u4efb\u52d9<\/a>\uff0c\u518d\u4f86<code>UseHangfireDashboard<\/code>\u662f\u5efa\u7acb\u5167\u5efa\u7684hangfire\u7684\u64cd\u4f5c\u756b\u9762\uff0c<code>GlobalConfiguration.Configuration.UseActivator<\/code>\u662f\u4f7f\u7528<code>autofac<\/code>\u7684\u6ce8\u5165\u7136\u5f8c\u76f8\u95dc\u7684\u985e\u5225<code>HangfireActivator<\/code>\u53ca<code>AllowAllConnectionsFilter<\/code>\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-cs\">\/\/\/ &lt;summary&gt;\n\/\/\/ hangfire activator\n\/\/\/ &lt;\/summary&gt;\npublic class HangfireActivator : JobActivator\n{\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ service provider\n    \/\/\/ &lt;\/summary&gt;\n    private readonly IServiceProvider _provider;\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Initializes a new instance of the &lt;see cref=&quot;HangfireActivator&quot; \/&gt; class.\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=&quot;provider&quot;&gt;service provider&lt;\/param&gt;\n    public HangfireActivator(IServiceProvider provider)\n    {\n        _provider = provider;\n    }\n\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ activate job\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=&quot;type&quot;&gt;active type&lt;\/param&gt;\n    \/\/\/ &lt;returns&gt;job instance&lt;\/returns&gt;\n    public override object ActivateJob(Type type)\n    {\n        return _provider.GetService(type);\n    }\n}\n\n\/\/\/ &lt;summary&gt;\n\/\/\/ for hangfire needless authorization\n\/\/\/ &lt;\/summary&gt;\npublic class AllowAllConnectionsFilter : IDashboardAuthorizationFilter\n{\n    \/\/\/ &lt;summary&gt;\n    \/\/\/ Authorize function\n    \/\/\/ &lt;\/summary&gt;\n    \/\/\/ &lt;param name=&quot;context&quot;&gt;dashboard context&lt;\/param&gt;\n    \/\/\/ &lt;returns&gt;is authorize&lt;\/returns&gt;\n    public bool Authorize(DashboardContext context)\n    {\n        \/\/ Allow outside. You need an authentication scenario for this part.\n        \/\/ DON&#039;T GO PRODUCTION WITH THIS LINES.\n        return true;\n    }\n}<\/code><\/pre>\n<p>\u5728\u8a3b\u518a\u73a9\u6392\u7a0b\u4e4b\u5f8c\uff0c\u63a5\u4e0b\u4f86\u8981<code>Startup.ConfigureServices<\/code>\u5167\u5beb\u8aaa\u8981\u4f7f\u7528\u6392\u7a0b\uff0c\u9019\u6642\u6211\u5011\u52a0\u5165\u4ee5\u4e0b\u5167\u5bb9\u3002<\/p>\n<pre><code class=\"language-cs\">services.AddHangfire(x =&gt; x.UseMemoryStorage());\nservices.AddHangfireServer();<\/code><\/pre>\n<p>\u9019\u88e1\u4f7f\u7528\u8a18\u61b6\u9ad4\u4f86\u653e\u7f6e\u6392\u7a0b\u76f8\u95dc\u8cc7\u6599\u4ee5\u53ca\u8f38\u5165\u53c3\u6578\u3002\u5230\u9019\u908a\u70ba\u6b62\u95dc\u65bc\u74b0\u5883\u7684\u8a2d\u5b9a\u5c31\u544a\u4e00\u6bb5\u843d\u3002\u63a5\u4e0b\u4f86\u8981\u518d\u8aaa\u660e\u5982\u4f55\u5efa\u7acb\u6392\u7a0b\u3002<\/p>\n<h3>\u5efa\u7acb\u6392\u7a0b<\/h3>\n<p>\u9019\u908a\u958b\u59cb\u8aaa\u660e\u5982\u4f55\u5efa\u7acb\u4e00\u500b\u6392\u7a0b\uff0c\u9019\u90e8\u5206\u900f\u904e<code>autofac<\/code>\u6ce8\u5165\u5f8c\u4f7f\u7528\u4e0a\u5176\u5be6\u76f8\u7576\u7c21\u55ae\uff0c\u5176\u5be6\u53ea\u8981\u5efa\u7acb\u4e00\u500b\u985e\u5225\u4e26\u516c\u958b\u4e00\u500b\u65b9\u6cd5\u5373\u53ef\uff0c\u9019\u908a\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u7576\u7570\u5e38\u767c\u751f\u6642hangfire\u6703\u6709\u91cd\u8a66\u7684\u6a5f\u5236\uff0c\u5982\u679c\u4e0d\u8981\u91cd\u8a66\u7684\u8a71\u8981\u5728\u8a72\u65b9\u6cd5\u4e0a\u52a0\u4e0aAttribute\u5373\u53ef\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-cs\">[AutomaticRetry(Attempts = 0)]\npublic void DoJob(DateTime datetime)<\/code><\/pre>\n<p>\u9084\u6709\u8981\u6ce8\u610f\u9019\u908a\u8f38\u5165\u7684\u53c3\u6578\u8981\u7c21\u55ae\uff0c\u82e5\u8f38\u5165\u7684\u53c3\u6578\u662f\u53c3\u8003\u578b\u5225\u7136\u5f8c\u6392\u7a0b\u6642\u9593\u53c8\u4e5d\u7684\u72c0\u6cc1\u4e0b\u6709\u53ef\u80fd\u8a72\u53c3\u6578\u88abgc\u4e86\uff0c\u9019\u6a23\u53ef\u5c31gg\u4e86\u3002\u6700\u5f8c\u4f86\u770b\u770b\u5982\u4f55\u4f7f\u7528\u6392\u7a0b\u3002<\/p>\n<h3>\u4f7f\u7528\u6392\u7a0b<\/h3>\n<p>\u9019\u908a\u5e38\u7528\u7684\u8a2d\u5b9a\u6392\u7a0b\u8a9e\u6cd5\u6709\u4e0b\u5217\u5169\u7a2e\u65b9\u5f0f\uff0c\u8acb\u770b\u4ee5\u4e0b\u7bc4\u4f8b\u3002<\/p>\n<pre><code class=\"language-cs\">RecurringJob.AddOrUpdate&lt;Schedule&gt;(&quot;DoJob&quot;,\n    x =&gt; x.DoJob(),\n    Cron.Daily(18));\n\nRecurringJob.AddOrUpdate&lt;Schedule&gt;(&quot;DoJob3&quot;,\n    x =&gt; x.DoJob(),\n    &quot;0\/30 * * * *&quot;);\n\nBackgroundJob.Schedule&lt;Schedule&gt;(&quot;DoJob2&quot;,\n    x =&gt; x.DoJob(),\n    TimeSpan.FromSeconds(3));<\/code><\/pre>\n<p>\u9996\u5148<code>RecurringJob.AddOrUpdate<\/code>\u662f\u5b9a\u671f\u6027\u7684\u6392\u7a0b\uff0c\u6240\u4ee5\u9019\u908a\u53ef\u4ee5\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cron\" title=\"Cron\u8868\u9054\u793a\" rel=\"nofollow noopener\" target=\"_blank\">Cron\u8868\u9054\u793a<\/a>\u6216\u662f\u4f7f\u7528Cron\u4f86\u7522\u751fCron\u8868\u9054\u793a\uff0c<code>BackgroundJob.Schedule<\/code>\u793a\u5ef6\u9072\u591a\u4e45\u5f8c\u89f8\u4e00\u4e00\u6b21\u6027\u7684\u6392\u7a0b\uff0c\u7136\u5f8c\u9084\u6709\u4e00\u7a2e<code>BackgroundJob.Enqueue<\/code>\u662f\u7acb\u5373\u89f8\u767c\u4e00\u6b21\u6027\u7684\u6392\u7a0b\u3002\u9019\u88e1\u8981\u6ce8\u610f\u7576\u6392\u7a0b\u7684\u53c3\u6578\u9700\u8981\u8f38\u5165\u6642\u4e0d\u8981\u4ee5\u4e0b\u5217\u65b9\u5f0f\u5beb\u3002<\/p>\n<pre><code class=\"language-cs\">BackgroundJob.Schedule&lt;Schedule&gt;(&quot;DoJob2&quot;,\n    x =&gt; x.DoJob(DateTime.Now),\n    TimeSpan.FromSeconds(3));<\/code><\/pre>\n<p>\u56e0\u70ba\u4ed6\u6703\u628a\u7576\u4e0b\u6642\u9593\u7d00\u9304\u7a0b\u6bcf\u6b21\u6392\u7a0b\u7684\u53c3\u6578\u4e4b\u4e00\uff0c\u6240\u4ee5\u5982\u679c\u9700\u8981\u6bcf\u6b21\u6392\u7a0b\u90fd\u9700\u8981\u8f38\u5165\u7576\u4e0b\u6642\u9593\u7684\u8a71\u5c31\u8981\u5efa\u7acb\u4e00\u500b\u65b9\u6cd5\u662f\u4e0d\u9700\u8981\u53c3\u6578\u8f38\u5165\u7684\uff0c\u4e26\u5728\u8a72\u65b9\u6cd5\u5167\u4f7f\u7528<code>DateTime.Now<\/code>\u624d\u5c0d\u3002<\/p>\n<h2>\u5f8c\u8a18<\/h2>\n<p>\u4eca\u5929\u8ddf\u8457\u5c0f\u5149\u9084\u6709\u5927\u982d\u5b78\u7fd2\u5982\u4f55\u900f\u904ehangfire\u4f86\u8a2d\u5b9a\u6392\u7a0b\uff0c\u4e26\u4e14\u5b78\u7fd2\u5230\u5982\u4f55\u5efa\u7acb\u4e00\u500b\u6392\u7a0b\u4ee5\u53ca\u900f\u904edi\u7684\u65b9\u5f0f\u628a\u9700\u8981\u7684\u5143\u4ef6\u7d66\u6ce8\u5165\uff0c\u5e0c\u671b\u5c0d\u5927\u5bb6\u7684dotnetcore\u6392\u7a0b\u8a2d\u8a08\u6709\u6240\u5e6b\u52a9\uff0c\u9019\u908a\u9084\u6709\u53e6\u5916\u4e00\u500b<a href=\"https:\/\/www.quartz-scheduler.net\/\" title=\"Quartz.NET\" rel=\"nofollow noopener\" target=\"_blank\">Quartz.NET<\/a>\u53e6\u5916\u4e00\u500b\u6392\u7a0b\u5957\u4ef6\uff0c\u7d66\u6709\u8208\u8da3\u7684\u4eba\u770b\u4e00\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u8655\u7406\u5b9a\u671f\u7684\u9700\u6c42 \u76f8\u4fe1\u5f88\u591a\u4eba\u6703\u9047\u5230\u9700\u8981\u5b9a\u671f\u505a\u67d0\u4e9b\u4e8b\u60c5\u7684\u72c0\u6cc1\uff0c\u4f8b\u5982\u6bcf\u5206\u9418\u53bb\u8a08\u7b97\u4e00\u6b21\u8cc7\u6599\uff0c\u6216\u8005\u4e00\u5206\u9418\u8ddf\u8cc7\u6599\u5eab\u6488\u53d6 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2021\/10\/07\/hangfire\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008D-08-\u6392\u7a0b\u8a2d\u5b9a ? hangfire\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[90,92,140],"class_list":["post-2439","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2021ironman","tag-dotnetcore","tag-hangfire"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=2439"}],"version-history":[{"count":15,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2439\/revisions"}],"predecessor-version":[{"id":2928,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/2439\/revisions\/2928"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=2439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=2439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=2439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}