ASP.Net Core 定时任务

https://procodeguide.com/programming/hangfire-in-aspnet-core-schedule-jobs/

安装Hangfire
dotnet add package Hangfire.AspNetCore

//这是将配置存在内存里
dotnet add package Hangfire.MemoryStorage

配置启动项Starup.cs

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.Configure<BookstoreDatabaseSettings>(Configuration.GetSection(nameof(BookstoreDatabaseSettings)));

            services.AddSingleton<IBookstoreDatabaseSettings>(sp =>
                sp.GetRequiredService<IOptions<BookstoreDatabaseSettings>>().Value);

            services.AddSingleton<BookServices>();

            //services.AddControllers();

            services.AddDbContext<EntityOracleDBContext.EntityOracleDBContext>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


            services.AddHangfire(configuration => configuration
                .UseMemoryStorage());//使用内存
            services.AddHangfireServer();//添加hangfire服务
            services.AddScoped<CronJob.CronJob>();//注册执行类


            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo()
                {
                    Title = "API",
                    Version = "V1"
                });
            });



            var provider = services.BuildServiceProvider();
            return provider;



        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,IServiceProvider provider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            //启用中间件服务生成Swagger作为JSON终结点
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
            });
            app.UseMvc();


            CronJob.CronJob mongodb = provider.GetService<CronJob.CronJob>();


            var options = new BackgroundJobServerOptions { SchedulePollingInterval = TimeSpan.FromSeconds(1.0) };//默认最小间隔为15s,这里设置成1s
            app.UseHangfireServer(options);//注入配置
            app.UseHangfireDashboard();//启动看板
            RecurringJob.AddOrUpdate(() => mongodb.syncs(), Cron.MinuteInterval(2),TimeZoneInfo.Local);//绑定触发器


        }

CronJob.cs

using HLZDVideoServer.Entities;
using HLZDVideoServer.MongoDBService;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HLZDVideoServer.CronJob
{
    public class CronJob
    {
        public  EntityOracleDBContext.EntityOracleDBContext context;
        private   BookServices _bookService;
        public CronJob(EntityOracleDBContext.EntityOracleDBContext context, BookServices bookService)
        {
            this.context = context;
            _bookService = bookService;
        }


        public  void syncs()
        {
            List<BB_FGSSC> bB_FGSSCs = this.context.BB_FGSSCs.ToList().Distinct().ToList();

            foreach (BB_FGSSC item in bB_FGSSCs)
            {
                _bookService.Create(item);
            }
        }
    }
}

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值