.NET Framework 4.7.2下 Hangfire 的集成(转载)
原文地址:.NET Framework 4.7.2下 Hangfire 的集成 - Sam Xiao - 博客园 (cnblogs.com)
.NET Framework 4.7.2下 Hangfire 的集成
参考资料:
开源的.NET定时任务组件Hangfire解析:https://www.cnblogs.com/pengze0902/p/6583119.html
.Net Core 简单的Hangfire部署Demo:https://blog.csdn.net/weixin_43925876/article/details/89257885
.NET之Hangfire快速入门和使用:https://www.cnblogs.com/Can-daydayup/p/11610747.html
一,先引入必要的dll
Hangfire.Core 1.7.11.0
Hangfire.MySql.Core 2.2.5.0
Microsoft.Owin.Host.SystemWeb 4.1.0.0
Hangfire.Dashboard.Authorization 3.0.0.0 用于登录时的密码
Microsoft.AspNet.WebApi.OwinSelfHost 在控制台程序中好寄存 网页
二,添加 Startup.cs 文件
一定要引入 Microsoft.Owin.Host.SystemWeb ,否则添加的 Startup 文件不会执行
1,在Startup.cs文件中引入名称空间
using Microsoft.Owin; using Owin; using Hangfire; using Hangfire.MySql.Core;
2,Web.config 文件中做数据库连接配置
<connectionStrings>
<!--Aceess数据库 providerName="System.Data.OleDb"
Oracle 数据库 providerName="System.Data.OracleClient"或者providerName="Oracle.DataAccess.Client"
SQLite数据库 providerName="System.Data.SQLite"
sql 数据库 providerName="System.Data.SqlClient"-->
<add name="coordinatordb" connectionString="Server=192.168.11.89;userid=root;password=Abc123;database=alibabacoordinatordb;Charset=utf8mb4;port=3306;Old Guids=true;AllowUserVariables=True;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
3,Startup.cs 人文件内容
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Hangfire;
using Hangfire.MySql.Core;
using Hangfire.Annotations;
using Hangfire.Client;
using Hangfire.Common;
using Hangfire.Dashboard.Owin;
using Hangfire.Dashboard.Pages;
using Hangfire.Dashboard.Resources;
using Hangfire.Logging.LogProviders;
using Hangfire.MySql;
using Hangfire.MySql.Core.JobQueue;
using Hangfire.Processing;
using Hangfire.Server;
using Hangfire.States;
using Hangfire.Storage.Monitoring;
[assembly: OwinStartup(typeof(Coordinator.MvcWebAPI.Startup))]
namespace Coordinator.MvcWebAPI
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// 有关如何配置应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=316888
//配置数据库连接
// 表前缀 TablePrefix = "hangfire"
// 数据库连接一定要加 AllowUserVariables=True;
// C# 连接 mySQL 出现 GUID 应包含带 4 个短划线的 32 位数 问题 在连接字符串中加入 Old Guids = true;
string coordinatordb = System.Configuration.ConfigurationManager.ConnectionStrings["coordinatordb"].ConnectionString;
GlobalConfiguration.Configuration.UseStorage(new MySqlStorage(coordinatordb, new MySqlStorageOptions() { TablePrefix = "hangfire" }));
app.UseHangfireDashboard(); //配置后台仪表盘
app.UseHangfireServer(); //开始使用Hangfire服务
}
}
}
然后启动项目。访问 https://localhost:44300/hangfire/ 其中,IP地址与端口按实际情况而定。
定时向应用程序发起请求,pingdom,防止IIS被回收。


浙公网安备 33010602011771号