轉載自:https://blog.csdn.net/u013711462/article/details/53449799
定時任務 Pomelo.AspNetCore.TimedJob
Pomelo.AspNetCore.TimedJob是一個.NET Core實現的定時任務job庫,支持毫秒級定時任務、從數據庫讀取定時配置、同步異步定時任務等功能。
由.NET Core社區大神兼前微軟MVP AmamiyaYuuko (入職微軟之后就卸任MVP…)開發維護,不過好像沒有開源,回頭問下看看能不能開源掉。
作者自己的介紹文章: Timed Job – Pomelo擴展包系列
Startup.cs相關代碼
我這邊使用的話,首先肯定是先安裝對應的包:Install-Package Pomelo.AspNetCore.TimedJob -Pre
然后在Startup.cs的ConfigureServices函數里面添加Service,在Configure函數里面Use一下。
// This method gets called by the runtime. Use this method to add services to the container.
publicvoidConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
//Add TimedJob services
services.AddTimedJob();
}
publicvoidConfigure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//使用TimedJob
app.UseTimedJob();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
Job相關代碼
接着新建一個類,明明為XXXJob.cs,引用命名空間using Pomelo.AspNetCore.TimedJob,XXXJob繼承於Job,添加以下代碼。
public class AutoGetMovieListJob:Job
{
// Begin 起始時間;Interval執行時間間隔,單位是毫秒,建議使用以下格式,此處為3小時;
//SkipWhileExecuting是否等待上一個執行完成,true為等待;
[Invoke(Begin = "2016-11-29 22:10", Interval = 1000 * 3600*3, SkipWhileExecuting =true)]
publicvoidRun()
{
//Job要執行的邏輯代碼
//LogHelper.Info("Start crawling");
//AddToLatestMovieList(100);
//AddToHotMovieList();
//LogHelper.Info("Finish crawling");
}
}
轉載自:http://www.jkeabc.com/432165.html

