I am using Hangfire 1.7.28 and Autofac 5.2.0. I also installed the nuget package Hangfire.Autofac.
Confirm the Hangfire.Autofac documentation is need to add: Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
to let Hangfire know about the DI.
Now i still got the No parameterless constructor fault. What am i missing in here?
Exception i got:
` System.MissingMethodException
No parameterless constructor defined for this object.
System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)
at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass10_0.<PerformJobWithFilters>b__0()
at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext context, IEnumerable`1 filters)
at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext context)
at Hangfire.Server.Worker.PerformJob(BackgroundProcessContext context, IStorageConnection connection, String jobId, BackgroundJob backgroundJob, IReadOnlyDictionary`2& customData)`
MyJob class: ``
`public interface IMyJob
{
void DoSomething();
}
public class MyJob : IMyJob
{
private readonly ILogger _logger;
public MyJob(ILogger logger)
{
_logger= logger;
}
public void DoSomething()
{
Console.WriteLine("Recurring!");
}
}`
// Startup.cs ``
` public void Configuration(IAppBuilder app)
{
// Configure DI dependancies
Bootstrapper.SetAutofacContainer(app);
// Configure authentication
ConfigureAuth(app);
// Configure Hangfire
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection"); var options = new DashboardOptions { Authorization = new[] { new HangfireAuthorizationFilter() } }; app.UseHangfireDashboard("/jobs", options); app.UseHangfireServer();
RecurringJob.AddOrUpdate<MyJob>("MYJOB", (x) => x.DoSomething(), Cron.MinuteInterval(5));
}`
// My Autofac bootstrapper class ``
`public class Bootstrapper
{
public static void SetAutofacContainer(IAppBuilder app)
{
var builder = new ContainerBuilder();
// Register Web controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// Register model binders that require DI.
builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
builder.RegisterModelBinderProvider();
// Register web abstractions like HttpContextBase.
builder.RegisterModule<AutofacWebTypesModule>();
// Enable property injection in view pages.
builder.RegisterSource(new ViewRegistrationSource());
// Enable property injection into action filters.
builder.RegisterFilterProvider();
// Identity
builder.RegisterType<ApplicationUserStore>().As<IUserStore<Medewerker, Guid>>().InstancePerLifetimeScope();
builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerLifetimeScope();
builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerLifetimeScope();
builder.Register<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerLifetimeScope();
builder.Register<IDataProtectionProvider>(c => app.GetDataProtectionProvider()).InstancePerLifetimeScope();
builder.RegisterType<Logger>().As<ILogger>().InstancePerLifetimeScope();
builder.RegisterType<MyJob>().AsSelf().InstancePerBackgroundJob();
// Build the container
var container = builder.Build();
Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
JobActivator.Current = new AutofacJobActivator(container);
// Replace the MVC dependancy resolver with Autofac
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Set the dependency resolver for MVC.
var mvcResolver = new AutofacDependencyResolver(container);
DependencyResolver.SetResolver(mvcResolver);
// Register the Autofac middleware FIRST, then the Autofac MVC middleware.
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
}
}`