我最近使用hangfire来处理冗长的工作,使我能够在ASP MVC Core应用程序中更有效地返回API调用 .

我通过在startup.cs中添加以下内容来表达我的意见

public class Startup
{

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()));
        services.AddHangfire(configuration => configuration.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseCors("AllowAll");

        app.UseMvc();

        app.UseHangfireDashboard();
        app.UseHangfireServer();     
    }

然后在控制器上的一个动作中调用它

using (new BackgroundJobServer())
                    {
                        formData.DateCreated = DateTime.Now;
                        formData.Source = "Web";
                        formData.StatusItem =  _context.Status.FirstOrDefault(x => x.Default == true);

                        _context.Lead.Add(formData);
                        _context.SaveChanges();
                    }

现在我需要每天凌晨1点发送一封电子邮件,其中包含数据库中的记录状态 .

我从以下角度对实施略有困惑:

  • 我在哪里实施后台工作? - 我把调用放在一个方法中,如果是这样的话怎么称为/ -I找不到BackgroundJob.AddOrUpdate,我理解它用于重复任务 - 调度方法需要一个时间 Span 对象,但所有例子都使用CRON .

我有所有代码来创建电子邮件,我只需要帮助安排它

感谢您的帮助 .