c# - asp.net 上的 HangFire 重复性工作

标签 c# asp.net hangfire

我开始使用 HangFire 每 X 分钟/小时处理一次任务,我需要添加重复性作业。在控制台应用程序中,我设法做得很好,但在 asp.net 上,我只能通过 BackgroundJob.Enqueue 方法让它工作一次。

public class Global : HttpApplication
{
    private static string LigacaoBD = myConn;
    private static ApiMethods sportRadar = new ApiMethods();
    private static Jogo jogo = new Jogo(LigacaoBD);
    private static List<SportRadar.ClassesCliente.Jogo> jogos;
    private static List<SportRadar.ClassesCliente.Competicao> competicoes;

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.UseSqlServerStorage(LigacaoBD);

        using (var connection = JobStorage.Current.GetConnection())
        {
            foreach (var recurringJob in connection.GetRecurringJobs())
            {
                RecurringJob.RemoveIfExists(recurringJob.Id);
            }
        }

        using (var server = new BackgroundJobServer())
        {
            // This works just fine
            var id=BackgroundJob.Enqueue(() => Actualizacoes());
            // This does not.
            // I even checked the DB for job queue or something but couldn't find anything
            RecurringJob.AddOrUpdate(id, () => Actualizacoes(), Cron.Minutely);
        }
    }



    public void Actualizacoes()
    {
        // Stuff I need to do regularly
    }
}

我以为我做对了,但显然我这里有问题。您认为问题出在哪里?

最佳答案

有两个问题。

一个。因为您正在传递 id - 这在您的实现中是不必要的。

BackgroundJob.Enqueue 返回作业的唯一标识符。在您的代码中,此作业已排队并执行。

改变

RecurringJob.AddOrUpdate(id, () => Actualizacoes(), Cron.Minutely);

RecurringJob.AddOrUpdate(() => Actualizacoes(), Cron.Minutely);

此外,您将 BackgroundJobServer 的创建包装在 using 语句中。然后在那里创建工作。因此,BackgroundJobServer 正在被 GC 清除。

相反,试试这个:

public class Global : HttpApplication
{
    private static string LigacaoBD = myConn;
    private static ApiMethods sportRadar = new ApiMethods();
    private static Jogo jogo = new Jogo(LigacaoBD);
    private static List<SportRadar.ClassesCliente.Jogo> jogos;
    private static List<SportRadar.ClassesCliente.Competicao> competicoes;

    private BackgroundJobServer _backgroundJobServer;

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        GlobalConfiguration.Configuration.UseSqlServerStorage(LigacaoBD);

        using (var connection = JobStorage.Current.GetConnection())
        {
            foreach (var recurringJob in connection.GetRecurringJobs())
            {
                RecurringJob.RemoveIfExists(recurringJob.Id);
            }
        }

        //create an instance of BackgroundJobServer
        _backgroundJobServer = new BackgroundJobServer();

        //add your recurring job
        RecurringJob.AddOrUpdate(() => Actualizacoes(), Cron.Minutely);
    }
}

关于c# - asp.net 上的 HangFire 重复性工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45801463/

相关文章:

postgresql - 为什么 HangFire 启动过程失败并出现此错误?

asp.net-core - 处理 Hangfire 内部的异常

c# - 对路径 'Global\{xxx}_YYY-YYY:13552' 的访问被拒绝。吊火?

c# - 静态接口(interface)等效 C#

javascript - 如何做这种确认对话框?

c# - 通过代码(C# 或 PowerShell)获取有关进程(如 Sysinternals Process Explorer)的 .NET 程序集信息

c# - 有没有一种方法可以检索类的文件名?

asp.net - Ubuntu 上的 Asp Net Core - 访问被拒绝

c# - 在客户端和 WCF 服务之间共享枚举?

c# - SQLite 插入带参数的字符串数据导致无效强制转换异常