Autofac, Hangfire with asp.net hangfire and Owin

I recently upgraded to a new version of Hangfire and I am struggeling trying to setup my webapi with autofac and Hangfire. I’m using Autofac Hangfire integration version 1.1 and Hangfire 1.4.2. I’m using Owin to host. I keep getting following error:

The requested service ‘Foo’ has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

Here is my owin startup configuration. All my registrations are made in the AutofacStandardModule class

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //we will have the firewall block all CE endpoints from the outside instead
        //ConfigureOAuthTokenConsumption(app);

        var storage = new SqlServerStorage("connection string");

        JobStorage.Current = storage;

        app.UseHangfireServer(new BackgroundJobServerOptions(),storage);
        app.UseHangfireDashboard("/Hangfire",new DashboardOptions(),storage);
        var builder = new ContainerBuilder();
        builder.RegisterModule(new AutofacStandardModule()); 
        var container = builder.Build();

        GlobalConfiguration.Configuration.UseAutofacActivator(container);
        }
}

Also, here is my web api config class. I dont see how I should be configuring Hangfire here also though…

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config, Autofac.Module moduleToAppend)
    {
        config.MapHttpAttributeRoutes();

        config.EnableCors();
        config.EnableSystemDiagnosticsTracing();

        var builder = new ContainerBuilder(); 

        builder.RegisterAssemblyTypes(
            Assembly.GetExecutingAssembly())
                .Where(t =>
                    !t.IsAbstract && typeof(ApiController).IsAssignableFrom(t))
                .InstancePerLifetimeScope();

        builder.RegisterModule(
            new AutofacStandardModule()); 

        if (moduleToAppend != null)
        {
            builder.RegisterModule(moduleToAppend);
        }

        var container = builder.Build();

        config.DependencyResolver = new AutofacWebApiDependencyResolver(
            container);

        //Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);

        //JobActivator.Current = new AutofacJobActivator(container);
        }
}

FWIW, I’m having the exact same problem as you describe here. I’m registering everything as normal in Autofac, but Hangfire acts as if the container it’s seeing doesn’t have these references wired up.

When I revert to a build from the 1.3 series of Hangfire, these activation problems disappear.

I thought this may have to do with the Autofac side of the equation, but I’ve updated all of the packages for this to their latest builds (Autofac 3.5.2, Autofac.Mvc5 3.3.4).

Instead of reverting to an old version, consider doing the following instead. I got it working after having changed to this

instead of…

jobClient.Enqueue(() => _foo.Bar(fooId, fooId2));

do this…

_jobClient.Enqueue<IFoo>(x => x.Bar(fooId, fooId2));