Hangfire autofac does not throw an exception when it's not able to resolve a service

I’m using hangfire to lauch background job but I have a problem when I try to use the hangfire autofac integration with generic to resolve automaticly the task service it’s not working because it’s is not able to be resolve one of the dependency. I suppose because I don’t receive any error.

 BackgroundJob.Enqueue<IBackgroundTask>(x => x.RunAsync() );  

If I use the other way around by resolving by myselft it’s working.

 var service = ApplicationContainer.Resolve<IBackgroundTask>();    
 BackgroundJob.Enqueue(() => service .RunAsync() );

I figured out that in my constructor I have a test service that cause the problem. If I remove the service in the constructor the service get resolved.

    public class BackgroundTask: IBackgroundTask
    {
      private readonly ILogger logger;
      private readonly ITest testService;

      public ConvertCarteCreditService(ILogger logger, **ITest test**)
      {
        this.logger = logger;
        this.testService = test;
        // this.testService = Startup.Resolve<ITest>();            
      }

I have configured autofac in the startup class like this :

var builder = new ContainerBuilder();

ServiceLayerInstaller.ConfigureServices(builder);			         
DataLayerInstaller.ConfigureServices(builder, connectionString,  readOnlyConnectionString);

builder.RegisterAssemblyTypes(typeof(WorkerRoleInstaller).
GetTypeInfo().Assembly).Where(t => t.Name.EndsWith("Test"))
.AsImplementedInterfaces();

WorkerRoleInstaller.ConfigureServices(builder);

builder.Populate(services);
ApplicationContainer = builder.Build();
        
var autofacJobActivator = new AutofacJobActivator(ApplicationContainer);       
GlobalConfiguration.Configuration.UseActivator(autofacJobActivator);

For debugging my problem, I have had the filter on the job execution so I can be able to get the exception in the OnStateElection. So, I get witch service wasn’t registred.

public class LogEverythingAttribute : JobFilterAttribute,
IClientFilter, IServerFilter, IElectStateFilter, IApplyStateFilter
{
private static readonly ILog Logger = LogProvider.GetLogger(“logger”);

    public void OnCreating(CreatingContext context)
    {
    }

    public void OnCreated(CreatedContext context)
    {
    }

    public void OnPerforming(PerformingContext context)
    {
    }

    public void OnPerformed(PerformedContext context)
    {
    }

    public void OnStateElection(ElectStateContext context)
    {
        var failedState = context.CandidateState as FailedState;
        if (failedState != null)
        {
            **Logger.WarnFormat(**

** "Job {0} has been failed due to an exception {1}",**
** context.BackgroundJob.Id,**
** failedState.Exception);**
}
}

    public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
    {
        ..
    }

    public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
    {
      ..
    }
}

I figure out after why my service wasn’t registred.

I hadn’t incorrectly define my ConfigureServices function in the StartUp.

Without returning the IServiceProvider, the registred dependency where not find during the resolve to job. I thougth that the job activator was enough for configuring autofac with hangfire.

        var autofacJobActivator = new AutofacJobActivator(ApplicationContainer, false);
        GlobalConfiguration.Configuration.UseActivator(autofacJobActivator);

public void ConfigureServices(IServiceCollection services)
{
// builder registration here …
ApplicationContainer = builder.Build();

        var configurationSection = Configuration.GetSection("LoggerInfo");

        SetupLogger(configurationSection.Get<LoggerInfo>(), connectionString);

        var autofacJobActivator = new AutofacJobActivator(ApplicationContainer, false);
        GlobalConfiguration.Configuration.UseActivator(autofacJobActivator);
  
    }

public IServiceProvider ConfigureServices(IServiceCollection services)
{
// builder registration here …
ApplicationContainer = builder.Build();

        var configurationSection = Configuration.GetSection("LoggerInfo");

        SetupLogger(configurationSection.Get<LoggerInfo>(), connectionString);

        var autofacJobActivator = new AutofacJobActivator(ApplicationContainer, false);
        GlobalConfiguration.Configuration.UseActivator(autofacJobActivator);

           **return new AutofacServiceProvider(ApplicationContainer);**
    }