Hangfire - No parameterless constructor defined for this object

Viewed 2099

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.

The whole Startup:

public void Configuration(IAppBuilder app)
{
    var httpConfiguration = new HttpConfiguration();

    ConfigureUnity(app, httpConfiguration);
    ConfigureWebApi(httpConfiguration);
    ConfigureAuth(app);

    //Set configuration into Owin
    app.UseWebApi(httpConfiguration);

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

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

}

public void ConfigureUnity(IAppBuilder app, HttpConfiguration config) {
    UnityContainer container = UnityConfig.GetConfiguredContainer() as UnityContainer;

    if (app.GetDataProtectionProvider() == null) {
        app.UseAesDataProtectionProvider();
    }

    IDataProtectionProvider dataProtectionProvider = app.GetDataProtectionProvider();

    container.RegisterInstance(dataProtectionProvider);


    app.CreatePerOwinContext(() =>
        UnityConfig.GetConfiguredContainer().Resolve<ApplicationUserManager‌​>()
    );

    config.DependencyResolver = new UnityDependencyResolver(UnityConfig.GetConfiguredContainer());

}

In this method GetConfiguredContainer is where I register my services and datastores.

3 Answers
Related