Hangfire throwing exceptions after app pool recycles

This is a really strange issue, so I’m going to do my best to explain it…

I’ve got hangfire all setup and I followed the instructions at http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html to make sure it is always running on the server. Everything works great locally, and works fine on the server… for a while. However, I started noticing that when I came in after the weekend and checked on it, all my jobs were throwing the exception:

System.MissingMethodException

Cannot create an instance of an interface.

What I’ve come to figure out is that this happens when the app pool recycles. To be honest, I don’t think I fully understand what is actually happening when we setup the “always running” stuff according to the documentation, so it’s difficult for me to troubleshoot this. I’m not sure if by following that guide the app pool is supposed to be prevented from recycling, or if that is something you can’t prevent and the code we added is just to bring everything back up after the recycle happens… I know though that if I manually recycle the app pool in IIS, I start getting the exception. It would make sense to me then that over time the app pool is automatically recycling and that is what causes hangfire to stop working. Strangely enough, it seems the only way I can get it to work again is to delete the bin folder on my server and republish.

Also, by the way, I’m using Autofac for my app and the Hangfire.Autofac package.

So I setup my ApplicationPreload and HangfireBootstrapper files exactly like they were in the documentation. I’m wondering though, do I have to do something in these files so that my IoC container is available to the jobs when HangfireBootstrapper.Instance.Start() is called?

Right now my Global.asax file looks like this:

    protected void Application_Start()
    {
        IocConfig.RegisterDependencies();
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AutoMapperConfig.RegisterMappings();
        BinderConfig.RegisterCustomModelBinders();

        HangfireBootstrapper.Instance.Start();
    }

    protected void Application_End(object sender, EventArgs e)
    {
        HangfireBootstrapper.Instance.Stop();
    }

The line IocConfig.RegisterDependencies() is where I do all my IoC registrations and that is also where I setup the JobActivater. So it looks like this:

public class IocConfig
{
    public static void RegisterDependencies()
    {
        var builder = new ContainerBuilder();
            
        // ... setup registrations

        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        JobActivator.Current = new AutofacJobActivator(container);
    }
}

If anyone can help me figure out what is going on it would be much appreciated. This problem has been plaguing me for weeks now and I can’t seem to figure it out. Like I said, I suspect I need to do something in the HangfireBootstrapper class to make my IoC container available but I’m not sure. Would just calling IocConfig.RegisterDependencies() in HangfireBootstrapper.Start() do the trick?

Thanks in advance!

A year and some months later and there is still no answer for this. I just ran into the same problem.

The issue, if it is like mine, is that you are using the ApplicationPreload to call HangfireBootstrapper.Instance.Start() when the app pool recycles (like in the documentation). If that’s the case, I figured out that you need to reinitialize the AutofacActivator with Hangfire; otherwise, it won’t know where to resolve the interfaces. You should move the JobActivator code into the HangfireBootstrapper class and have it get the container either by rebuilding it or by passing it around as a static variable.

Hope this helps someone!

2 Likes

Thanks for this. Had the same issue with an always running instance and was initialising the AutofacActivator in Application_Start and not the Bootstrapper.

1 Like