如果我理解正确,您的解决方案中的控制台就像一个“伪”HangFire,因为就像您说的那样,它会超时执行一些数据库操作,并且您计划使用任务计划程序来执行它。
HangFire 概述
HangFire 旨在通过控制台应用完全满足您的需求,但具有更多功能和功能,因此您可以避免自己创建所有这些的所有开销。
HangFire 安装
HangFire 通常与 ASP.NET 应用程序一起安装,但如果您仔细阅读文档,您会惊奇地发现:
Hangfire 项目由几个可用的 NuGet 包组成
NuGet 库站点。这是您应该知道的基本软件包列表
关于:
-
Hangfire – 仅用于安装的引导程序包
适用于使用 SQL Server 作为作业存储的 ASP.NET 应用程序。它
简单地引用 Hangfire.Core、Hangfire.SqlServer 和
Microsoft.Owin.Host.SystemWeb 包。
-
Hangfire.Core – 基本包
包含 Hangfire 的所有核心组件。它可以用于任何
项目类型,包括 ASP.NET 应用程序、Windows 服务、控制台、
任何与 OWIN 兼容的 Web 应用程序、Azure Worker Role 等。
如您所见,HangFire 可用于任何类型的项目,包括控制台应用程序,但您需要根据将使用的作业存储类型来管理和添加所有库。 See more here:
安装 HangFire 后,您可以将其配置为使用仪表板,这是一个界面,您可以在其中找到有关后台作业的所有信息。在我工作的公司中,我们多次使用 HangFire 来处理重复性工作,主要用于导入用户、跨应用程序同步信息以及执行在工作时间运行成本很高的操作,当我们想知道是否某个作业是否正在运行。它还使用CRON 来安排操作。
我们现在使用的一个示例是:
Startup.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
//Get the connection string of the HangFire database
GlobalConfiguration.Configuration.UseSqlServerStorage(connection);
//Start HangFire Server and enable the Dashboard
app.UseHangfireDashboard();
app.UseHangfireServer();
//Start HangFire Recurring Jobs
HangfireServices.Instance.StartSendDetails();
HangfireServices.Instance.StartDeleteDetails();
}
}
HangfireServices.cs
public class HangfireServices
{
//.. dependency injection and other definitions
//ID of the Recurring JOBS
public static string SEND_SERVICE = "Send";
public static string DELETE_SERVICE = "Delete";
public void StartSend()
{
RecurringJob.AddOrUpdate(SEND_SERVICE, () =>
Business.Send(), //this is my class that does the actual process
HangFireConfiguration.Instance.SendCron.Record); //this is a simple class that reads an configuration CRON file
}
public void StartDeleteDetails()
{
RecurringJob.AddOrUpdate(DELETE_SERVICE, () =>
Business.SendDelete(), //this is my class that does the actual process
HangFireConfiguration.Instance.DeleteCron.Record); //this is a simple class that reads an configuration CRON file
}
}
HangFireConfiguration.cs
public sealed class HangFireConfiguration : ConfigurationSection
{
private static HangFireConfiguration _instance;
public static HangFireConfiguration Instance
{
get { return _instance ?? (_instance = (HangFireConfiguration)WebConfigurationManager.GetSection("hangfire")); }
}
[ConfigurationProperty("send_cron", IsRequired = true)]
public CronElements SendCron
{
get { return (CronElements)base["send_cron"]; }
set { base["send_cron"] = value; }
}
[ConfigurationProperty("delete_cron", IsRequired = true)]
public CronElements DeleteCron
{
get { return (CronElements)base["delete_cron"]; }
set { base["delete_cron"] = value; }
}
}
hangfire.config
<hangfire>
<send_cron record="0,15,30,45 * * * *"></send_cron>
<delete_cron record="0,15,30,45 * * * *"></delete_cron>
</hangfire>
上面的 CRON 表达式将在每天每小时 0,15,30,45 分钟运行。
Web.config
<configSections>
<!-- Points to the HangFireConfiguration class -->
<section name="hangfire" type="MyProject.Configuration.HangFireConfiguration" />
</configSections>
<!-- Points to the .config file -->
<hangfire configSource="Configs\hangfire.config" />
结论
鉴于您描述的场景,我可能会在您的 ASP.NET MVC 应用程序中安装 HangFire 并删除控制台应用程序,这很简单,因为它是一个不用担心的项目。即使您可以将它安装在控制台应用程序上,我也不愿意走这条路,因为如果您碰壁(相信我,您会碰壁的),您很可能会在大多数情况下找到帮助对于安装在 ASP.NET 应用程序中的情况。