hangfire.mysql.core_asp.net core 3.1+mysql8.0+Hangfire遇到的异常解决记

本文记录了在ASP.NET Core 3.1环境下使用hangfire集成mysql 8.0.12时遇到的异常,包括关于'Parameter '@rownum' 必须定义的错误和SQL语法错误。通过在连接字符串中添加AllowUserVariables=True;解决了第一个问题。由于Hangfire.MySqlStorage库已过时,建议切换到新的存储库,并展示了配置方式。此外,还提供了因表不存在引发的异常解决方案,给出了最新的数据库脚本。
摘要由CSDN通过智能技术生成

Hangfire大家都很熟了,在.net 下用处很多,最近在asp.net core 3.1 使用hangfire的过程中,遇到很多问题,现在记录下来,分享给下一位。

1.现在的开发环境:

asp.net core 3.1

hangfire.aspnetcore 1.7.9

mysql 8.0.12

hangfire.mysql.core 2.2.5

2.遇到的第一个异常解决:

{"code":401,"data":"","message":"Parameter '@rownum' must be defined. To use this as a variable, set 'Allow User Variables=true' in the connection string."}

这个我查了一些资料,解决办法是在连接字符串添加AllowUserVariables=True;得以解决

你的连接字符串看起来应该像这样:

Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;

AllowUserVariables=True;

网上搜索到的,有提示变量为Allow User Variables=true(注意中间有空格),经我实践是不行的,需要使用AllowUserVariables=True;(中间没有空格) 这样的方式。

3.遇到下面的类似的异常:

{"code":401,"data":"","message":"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rank\r\n\t from `Hangfire_Set`,\r\n (select @rownum := 0) r \r\n w' at line 3"}

经查,原来我们是引用:Hangfire.MySqlStorage

这个库来实现mysql版本下的hangfire数据持久化。这个库现在已经不实用了。

建议换成:

这个库

4.换成以上库以后,使用方式如下:

services.AddHangfire(x => x.UseStorage(new MySqlStorage(Configuration.GetConnectionString("Hangfire"),new MySqlStorageOptions(){TablePrefix = "Custom"})));

支持表前缀为custom,不需要添加下划线,表会自动创建custom_的表。

5.以下异常:

MySqlException: Table ' custom__distributedlock' doesn't exist

要注意,表脚本要跟着修改一下:

从原来12张表新增了一张表,变为13张表,

增加了表:DistributedLock

github上有脚本,为了方便大家,我把脚本贴一下,直接用这个修改就可以了:

-- ----------------------------

-- Table structure for `Job`

-- ----------------------------

CREATE TABLE `_Job` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`StateId` int(11) DEFAULT NULL,

`StateName` varchar(20) DEFAULT NULL,

`InvocationData` longtext NOT NULL,

`Arguments` longtext NOT NULL,

`CreatedAt` datetime NOT NULL,

`ExpireAt` datetime DEFAULT NULL,

PRIMARY KEY (`Id`),

KEY `IX_Job_StateName` (`StateName`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `Counter`

-- ----------------------------

CREATE TABLE `_Counter` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`Key` varchar(100) NOT NULL,

`Value` int(11) NOT NULL,

`ExpireAt` datetime DEFAULT NULL,

PRIMARY KEY (`Id`),

KEY `IX_Counter_Key` (`Key`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `_AggregatedCounter` (

Id int(11) NOT NULL AUTO_INCREMENT,

`Key` varchar(100) NOT NULL,

`Value` int(11) NOT NULL,

ExpireAt datetime DEFAULT NULL,

PRIMARY KEY (`Id`),

UNIQUE KEY `IX_CounterAggregated_Key` (`Key`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `DistributedLock`

-- ----------------------------

CREATE TABLE `_DistributedLock` (

`Resource` varchar(100) NOT NULL,

`CreatedAt` datetime NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `Hash`

-- ----------------------------

CREATE TABLE `_Hash` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`Key` varchar(100) NOT NULL,

`Field` varchar(40) NOT NULL,

`Value` longtext,

`ExpireAt` datetime DEFAULT NULL,

PRIMARY KEY (`Id`),

UNIQUE KEY `IX_Hash_Key_Field` (`Key`,`Field`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `JobParameter`

-- ----------------------------

CREATE TABLE `_JobParameter` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`JobId` int(11) NOT NULL,

`Name` varchar(40) NOT NULL,

`Value` longtext,

PRIMARY KEY (`Id`),

CONSTRAINT `IX_JobParameter_JobId_Name` UNIQUE (`JobId`,`Name`),

KEY `FK_JobParameter_Job` (`JobId`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `JobQueue`

-- ----------------------------

CREATE TABLE `_JobQueue` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`JobId` int(11) NOT NULL,

`Queue` varchar(50) NOT NULL,

`FetchedAt` datetime DEFAULT NULL,

`FetchToken` varchar(36) DEFAULT NULL,

PRIMARY KEY (`Id`),

INDEX `IX_JobQueue_QueueAndFetchedAt` (`Queue`,`FetchedAt`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `JobState`

-- ----------------------------

CREATE TABLE `_JobState` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`JobId` int(11) NOT NULL,

`Name` varchar(20) NOT NULL,

`Reason` varchar(100) DEFAULT NULL,

`CreatedAt` datetime NOT NULL,

`Data` longtext,

PRIMARY KEY (`Id`),

KEY `FK_JobState_Job` (`JobId`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `Server`

-- ----------------------------

CREATE TABLE `_Server` (

`Id` varchar(100) NOT NULL,

`Data` longtext NOT NULL,

`LastHeartbeat` datetime DEFAULT NULL,

PRIMARY KEY (`Id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ----------------------------

-- Table structure for `Set`

-- ----------------------------

CREATE TABLE `_Set` (

`Id` int(11) NOT NULL AUTO_INCREMENT,

`Key` varchar(100) NOT NULL,

`Value` varchar(256) NOT NULL,

`Score` float NOT NULL,

`ExpireAt` datetime DEFAULT NULL,

PRIMARY KEY (`Id`),

UNIQUE KEY `IX_Set_Key_Value` (`Key`,`Value`)

) ENGINE=InnoDB CHARSET=utf8mb4;

CREATE TABLE `_State`

(

Id int(11) NOT NULL AUTO_INCREMENT,

JobId int(11) NOT NULL,

Name varchar(20) NOT NULL,

Reason varchar(100) NULL,

CreatedAt datetime NOT NULL,

Data longtext NULL,

PRIMARY KEY (`Id`),

KEY `FK_HangFire_State_Job` (`JobId`)

) ENGINE=InnoDB CHARSET=utf8mb4;

CREATE TABLE `_List`

(

`Id` int(11) NOT NULL AUTO_INCREMENT,

`Key` varchar(100) NOT NULL,

`Value` longtext NULL,

`ExpireAt` datetime NULL,

PRIMARY KEY (`Id`)

) ENGINE=InnoDB CHARSET=utf8mb4;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。关于 HangfireMySQLASP.NET Core 3.1 的使用,我可以给出一些异常解决的建议。 首先,确保你已经按照官方文档正确地安装和配置了 HangfireMySQL。然后,根据你遇到异常,尝试以下解决方法: 1. "Could not load file or assembly 'MySql.Data' or one of its dependencies" 异常 这个异常通常是由于缺少 MySQL 数据库驱动程序引起的。你需要在项目中添加对 MySQL 数据库驱动程序的引用。可以通过 NuGet 包管理器来安装 MySQL.Data。 2. "Specified key was too long; max key length is 767 bytes" 异常 这个异常是由于 MySQL 的索引长度限制引起的。解决方法是,在你的 DbContext 类中覆盖 OnModelCreating 方法,将所有字符串属性的最大长度设置为 255。 ``` protected override void OnModelCreating(ModelBuilder modelBuilder) { foreach (var property in modelBuilder.Model.GetEntityTypes() .SelectMany(t => t.GetProperties()) .Where(p => p.ClrType == typeof(string))) { property.SetMaxLength(255); } } ``` 3. "MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding" 异常 这个异常通常是由于 MySQL 连接超时引起的。解决方法是,在连接字符串中添加 Connection Timeout 参数,例如: ``` "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;Connection Timeout=60;" ``` 这将使连接超时时间为 60 秒。 希望这些解决方法能帮助你解决异常问题。如果你还有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值