InstancePerBackgroundJob() issue in Hangfire 1.5.0-beta2

I’m working on integrating Hangfire into our product using Autofac as an IoC container, and wish to use the InstancePerLifetimeScope(). However, when I register types using the builder, I get the following exception:

Method not found: 'Autofac.Builder.IRegistrationBuilder`3<!0,!1,!2> Autofac.Builder.IRegistrationBuilder`3.InstancePerMatchingLifetimeScope(System.Object)'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: Method not found: 'Autofac.Builder.IRegistrationBuilder`3<!0,!1,!2> Autofac.Builder.IRegistrationBuilder`3.InstancePerMatchingLifetimeScope(System.Object)'.

Source Error: 
Line 36: 
Line 37:             // register test job class
Line 38:             builder.RegisterType<HangfireTest>()
Line 39:                 .As<IHangfireTest>()
Line 40:                 .InstancePerBackgroundJob();

I’m trying to figure out where I’m going wrong. If I register the type as InstancePerLifetimeScope(), everything works, and the job resolves the type correctly and executes correctly:


Any suggestions on what I should be looking for? Is there additional configuration required to use the InstancePerBackgroundJob scope? Is this potentially a bug in the beta version?

I’m using the following packages:

Autofac 3.5.2
HangFire.Core 1.5.0-beta2
HangFire.SqlServer 1.5.0-beta2
HangFire.Autofac 1.2.0-beta1

This appears to be an issue with the Hangfire.Autofac 1.2.0-beta1 build. When I remove that package, and re-implement the functionality myself, the InstancePerBackgroundJob scope works correctly.

@odinserj This is probably the same issue with Hangfire.Autofac with Autofac 4.0.0-beta*

Sounds like a bug in Hangfire.Autofac

Is InstancePerBackgroundJob something new in hanfire, ie 1.5 beta? I cannot find it this extensionmethod. I used hangfire.core 1.4.2 and just upgraded to 1.4.5 but still I cannot find it… I have the autofac integration installed as well, but the 1.1 version, not the 1.2

It’s part of Hangfire.Autofac

Thank you for this bug report. The problem was with the InstancePerMatchingLifetimeScope(System.Object) method that was changed in Autofac 3.0.0 to InstancePerMatchingLifetimeScope(params System.Object[]). I’ve released two new versions:

  • 1.2.0-beta2 that supports [2.6,3.0.0).
  • 2.0.0-beta1 that supports 3.0.0 and higher.