在.NET Core环境中保持HangFire应用程序在IIS上清醒

时间:2017-07-31 23:19:19

标签: hangfire

我遇到困难:IApplicationLifetime取消等待: ApplicationStarted, ApplicationStopped 在我的.NET核心应用程序中使用HangFire批处理应用程序。

传递给ApplicationStarted.Register()和ApplicationStopped.Register()的委托不会被调用。我使用的是.NET Core 1.0版。

在.NET Core中使用以下解决方法(对应用程序使用适当的配置,在Startup.Configure(..)方法中注入您自己的“logger”)。注意RestartURL:URL是应用程序的URL - 轻量级命中。

保持批处理活动的当前解决方法:

var batchProcessKeepAliveTimer = new Timer();
        batchProcessKeepAliveTimer.Elapsed += delegate
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(Configuration["Data:RestartURL:URL"]);
            System.Net.WebResponse resp = req.GetResponse();
            logger.Log("Polling batch restart 60 milli second intervals");
        };
        batchProcessKeepAliveTimer.Enabled = true;
        double result = 60000;//default value in milliseconds
        if (Double.TryParse(Configuration["Data:RestartURL:PollingInterval"], out result))
            batchProcessKeepAliveTimer.Interval = result;

缺点是:当手动关闭并重新启动应用程序池时,不保证立即调用启动。我发现我需要发出一个显式的HTTP请求,以便应用程序池可以重新实例化Startup对象。换句话说,我需要进行一次HTTP调用来引导这个计时器。

但是,如果启动已经像这样实例化,那么计时器可以持续很长时间,直到它关闭。如果定时器间隔小于IdleTime,则应用程序的维护工作量会减少。

任何人都有更好的工作方法 - 特别是如上所述使用.NET核心取消令牌?

0 个答案:

没有答案