0

I have integrated hangfire in to Asp.net web application and trying to use session variables in to Hangfire Recurring Job as like below :

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));

    }
}

My “HangfireSyncServices” class as below:

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...
}

but I am getting run time exception as below at the time of getting value in to “userName”: Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the section in the application configuration.

I have tried to resolve above error using this LINK & other solution also but not able to resolved yet. can anyone help me on this issue.

Thanks in advance,
Hiren

  • That doesn't make sense that you'd want to use session state in a Hangfire job. The whole point of Hangfire jobs is that they run in the background. Therefore they won't be tied to a session. You're going to need to rethink the architecture. – mason Apr 17 '18 at 19:10

1 Answers1

0

Hangfire jobs don't run in the same context as asp.net, it has it's own thread pool. In fact, Hangfire jobs may even execute on a different server than the one that queued the job if you have multiple servers in your hangfire pool.

Any data that you want to have access to from within the job needs to be passed in as a method parameter. For example:

public partial class HangfireSyncServices //: APIPageClass <- you can't do this..
{
    public void ADDRESS_SYNC(string userName)
    {
        //........Rest code.......
    }
}

string userName = Convert.ToString(Session[Constants.Sessions.LoggedInUser]).ToUpper();
RecurringJob.AddOrUpdate("ADDRESS_SYNC", () => objSync.ADDRESS_SYNC(userName), Cron.MinuteInterval(30));

Note that doing the above creates a recurring task that will always execute for the same user, the one that was triggered the web request that created the job.

Next problem: you're trying to create this job in the server startup, so there is no session yet. You only get a session when a web request is in progress. I can't help you with that because I don't have any idea what you're actually trying to do.

caesay
  • 16,932
  • 15
  • 95
  • 160