.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被回收。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2019-09-11 按Excel的模板导出数据
2019-09-11 泛型和DataTable的属性