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