Hi all,
I’m really struggling with getting these two to work together. I have followed the instructions for injection/IoC in the hangfire docs and in the Hangfire.Ninject docs, but I can’t get the project to build, getting the error '‘TaskManager.RegistrationManager’ does not contain a constructor that takes 0 arguments. Ninject is working everywhere else in my MVC 5 app
Ok, so my setup: MVC 5 app .net 4.5
Ninject: I’m using WebActivatorEx to bootstrap ninject and hook up my IDependencyResolver class to the MVC dependency resolver property. The class is pretty straightforward:
public class NinjectDependencyResolver:IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
//bind action attribute to action filter for IsAdmin
kernel.BindFilter<IsAdminFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<IsAdminAttribute>()
.WithConstructorArgumentFromActionAttribute<IsAdminAttribute>(
"fullOnly",
att => att.FullAdminOnly);
kernel.BindFilter<TimeSensitiveFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<TimeSensitiveAttribute>()
.WithConstructorArgumentFromActionAttribute<TimeSensitiveAttribute>(
"returnJson",
att => att.ReturnJson);
kernel.Bind<ILog>().ToMethod(GetLog);
kernel.Bind<ILogConfig>().To<Logging.LogConfig>();
kernel.Bind<ISystemUserFactory>().To<SystemUserFactory>();
kernel.Bind<ISystemUser>().To<SystemUser>();
kernel.Bind<IESConfigCommands>().To<ESCommands>();
kernel.Bind<IAuthUtils>().To<AuthUtils>();
kernel.Bind<IESStudentCommands>().To<ESCommands>();
kernel.Bind<IESConfigurationManager>().To<ESConfigurationManager>();
kernel.Bind<IESStudentCommands>().To<ESCommands>();
kernel.Bind<IESTemplateEmailService>().To<ESTemplateEmailService>();
kernel.Bind<IEmail>().To<Email.Email>();
kernel.Bind<ITxnQueueManager>().To<TxnQueueManager>();
}
private static ILog GetLog(IContext ctx)
{
return Config.ConfigHelper.Log;
}
}
Hangfire: This is bootstrapped in the OWIN pipeline as so:
public void Configuration(IAppBuilder app)
{
_kernel = new Ninject.Web.Common.Bootstrapper().Kernel;
Hangfire.GlobalConfiguration.Configuration.UseNinjectActivator(_kernel);
StartHangFireJobs(app);
...
}
private void StartHangFireJobs(IAppBuilder app)
{
Hangfire.GlobalConfiguration.Configuration
.UseSqlServerStorage("HangefireDB");
var serverOptions = new BackgroundJobServerOptions
{
Queues = new[] { Config.ConfigHelper.QueueName.ToLower().Split(' ')[0] }
};
app.UseHangfireServer();
app.UseHangfireDashboard();
...
}
As I mentioned before, I know Ninject is working correctly as it’s injecting into my controllers no problem. But when I try to use a class for my recurring job, dependencies I have bound are not being injected. My job class constructor is:
public RegistrationManager(
IESConfigurationManager configMgrParam,
IESStudentCommands studentCmdsParam,
IESTemplateEmailService emailTemplateServiceParam,
IEmail emailServiceParam,
ITxnQueueManager txnMgrParam)
{
_configMgr = configMgrParam;
_studentCmd = studentCmdsParam;
_emailTemplateService = emailTemplateServiceParam;
_mail = emailServiceParam;
_sysFromAddress = _configMgr.GetAppSetting(Globals.CONFIG_SYS_FROM_ADDRESS);
}
Can anyone help? I’m hitting a brick wall here.