Hangfire - No parameterless constructor defined for this object

I am using Hangfire to do some recurring jobs, but I’m getting the following exception:

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.RuntimeType.CreateInstanceDefaultCtor(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.ActivateJob(Type jobType)
   at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(Type type)
   at Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext context)
   at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_0.<PerformJobWithFilters>b__0()
   at Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter filter, PerformingContext preContext, Func`1 continuation)
   at Hangfire.Server.BackgroundJobPerformer.<>c__DisplayClass8_1.<PerformJobWithFilters>b__2()
   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)

I have initialized my Hangfire in my AppBuilder:

  Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);

            app.UseHangfireDashboard();
            app.UseHangfireServer();

Basically, in my application you can create contracts. Everytime a contract is created, I am adding a new recurring job, using the following line:

RecurringJob.AddOrUpdate(() => AutomaticMovementsCreation(contractToCreate.DebitValue, contractId, descriptionId.Result), Cron.Minutely);

This is the construtor of the class where the RecurringJob is being added:

public ContractServiceImpl(IAuthService authService, IContractStore contractStore)
    {
        _contractStore = contractStore;
        _authService = authService;
    }

The _contractStore is being used by the method AutomaticMovementsCreation.

I think it is something due to dependy injection, but I really do not know what is exacly and how to solve it. By the way I am using an Unity Container to register my application dependencies.

Hi @Rui_Queiros!

Could you tell if you use any IoC container in your project? If yes, then you can find useful information in Using IoC containers tutorial in Hangfire docs.

1 Like

Already solved this:

    Hangfire.GlobalConfiguration.Configuration.UseActivator(new ContainerJobActivator(container));
    Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings[ConfigurationKeys.DbDefaultConnectionName].ConnectionString);
    app.UseHangfireDashboard();
    app.UseHangfireServer();
1 Like

Hi, Can you please post your ContainerJobActivator Class. I could not resolve this container in ContainerJobActivator Class

Pick the IoC you are using and use the package associated with it. https://www.hangfire.io/extensions.html#ioc-containers