【发布时间】:2018-04-17 19:00:06
【问题描述】:
我已将 hangfire 集成到 Asp.net Web 应用程序中,并尝试在 Hangfire Recurring Job 中使用会话变量,如下所示:
public class Startup
{
public void Configuration(IAppBuilder app)
{
HangfireSyncServices objSync = new HangfireSyncServices();
var options = new DashboardOptions
{
Authorization = new[] { new CustomAuthorizationFilter() }
};
app.UseHangfireDashboard("/hangfire", options);
app.UseHangfireServer();
//Recurring Job
RecurringJob.AddOrUpdate("ADDRESS_SYNC", () => objSync.ADDRESS_SYNC(), Cron.MinuteInterval(30));
}
}
我的“HangfireSyncServices”类如下:
public partial class HangfireSyncServices : APIPageClass
{
public void ADDRESS_SYNC()
{
string userName = Convert.ToString(Session[Constants.Sessions.LoggedInUser]).ToUpper();
//Exception throwing on above statement..
//........Rest code.......
}
}
public abstract class APIPageClass : System.Web.UI.Page
{
//common property & methods...
}
但是在获取“用户名”的价值时,我遇到了如下运行时异常: 会话状态只能在 enableSessionState 设置为 true 时使用,无论是在配置文件中还是在 Page 指令中。还请确保 System.Web.SessionStateModule 或自定义会话状态模块包含在 应用程序配置中的部分。
我已尝试使用此LINK 和其他解决方案来解决上述错误,但尚未解决。谁能帮我解决这个问题。
提前致谢,
雇人
【问题讨论】:
-
您想在 Hangfire 作业中使用会话状态是没有意义的。 Hangfire 作业的全部意义在于它们在后台运行。因此,它们不会被绑定到会话。您将需要重新考虑架构。