DataContext Old

Hey
So I have this weird problem that if I change a record and do something else with hangfire, the hangfire job uses the old data. I am using autofac but I’m a bit lost on what I need to configure to have this work correctly.

If I run the code out of hangfire then everything works fine. I do have the datacontext as InstancePerLifeTimeScope
and the jobservice that runs the hangfirejobs as .InstancePerRequest(AutofacJobActivator.LifetimeScopeTag)

I noticed this link http://stackoverflow.com/questions/27961210/hangfire-dependency-injection-lifetime-scope but wasn’t sure if this was still relevant as it’s a little older.

thanks

Hi @peetah! Could you post here the source code of your related background jobs, the code responsible for type registration in your container, and the code that is creating background jobs?

In general, for each background job, a new connection should be established for each background job, and a new query should be issued against your database to query the needed entity (whose Id should be passed to your background job).

Not helping but just to say I saw a problem where Hangfire jobs were cached somewhere and I couldn’t find where - I could delete them from the database, stop and start the site and they’d just come back, so there is something other than the job storage in the database at work…

Never got to the bottom of it, as it disappeared fine from our integration server!

Have you found a solution to this problem. It behaves like Hangfire caches assembly or metadata of classes (ctor in my case). Ctor of a class changed, but still HF tries to resolve the class based on old ctor dependencies.

How many ctors does your class have? Is it choosing the default ctor instead of one with DI classes? Hangfire doesn’t do anything fancy to new up your class. It has a JobActivator class which handles instance creation and by default it will use the default ctor. Chances are if the wrong ctor is being chosen, it’s something with your DI setup. I’ve used the Autofac library before to setup a JobActivator for using Autofac as my IoC.

I think one thing that often gets left out is just because your IoC is functioning in your main webapp, doesn’t mean it’s setup properly for Hangfire. Without also having a JobActivator for your IoC library and setting that as the activator for your background server, you won’t get the results you are expecting.