I'm having a problem activating instance of my class with no parametless constructor defined.
The constructor:
public HangfireExecutor(ICommandDispatcher commandDispatcher, IQueryDispatcher queryDispatcher, IMapper mapper)
How I register and configure Hangfire (three dots are used instead of sensitive info):
[assembly: OwinStartupAttribute(typeof(Web2.Startup))]
public partial class Startup
private IAppBuilder _app;
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
_app = app;
GlobalConfiguration.Configuration.UseSqlServerStorage("...");
_app.UseHangfireDashboard("/...", new DashboardOptions
{
Authorization = new[] { new HangfireDashboardAuthorizationFilter() },
AppPath = "/Identity/Create"
});
_app.UseHangfireServer();
_app.UseNinjectMiddleware(CreateKernel);
}
Registration in IoC container:
public partial class Startup
{
...
protected IKernel CreateKernel()
{
var kernel = new StandardKernel();
...
kernel.Bind<HangfireExecutor>().ToSelf().InBackgroundJobScope();
GlobalConfiguration.Configuration.UseNinjectActivator(kernel);
return kernel;
The error:
System.MissingMethodException
No parameterless constructor defined for this object hangfire ninject System.RuntimeTypeHandle.CreateInstance
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)
For me it looks like the Hangfire does not use Ninject activator (?) but I don't know why.
I've followed both tutorials: on Hangfire site and Hangfire.Ninject github and several github repos and SO questions.
Instatiating other classes not used by Hangfire works well; also instatiating Hangfire executor with parametless constructor works properly.
I'm using:
- ASP .NET MVC 5
- .NET Framework 4.6.1,
- Hangfire 1.6.21
- Hangfire.Ninject 1.2
As method
_app.UseNinjectMiddleware(CreateKernel);
does not create kernel (just keeps delegate to the metod creating the kernel) the correct order of commands in Hangfire configuration in my case should be:and then at the end of
CreateKernel
method:Now Hangfire started resolving dependencies. I think that it is important to create kernel as soon after starting the app as possible - otherwise Hangfire may not be initialized and background jobs will not be executed.