Hangfire out of memory error with ninject


Any help or insight will save my day and will be greatly appreciated!!

I have an ASP.NET MVC application which uses ninject as the IOC container. I see some strange performance issues when running a job on the background. When running the same job within the ASP.NET application, things are normal.

So, if I run directly in Application_Start() like this, everything is fine

var schedulerService = DependencyResolver.Current.GetService();

But when run through hangfire, the heap is filled with tons of objects and results in out of memory exception. Not sure why it is allocating so many objects all of a sudden.

var kernel = new Ninject.Web.Common.Bootstrapper().Kernel;
BackgroundJob.Enqueue((q) => q.RunTasks(context));

What am I doing wrong? Anyone has experienced anything like this?

I tried to bypass hangfire and just did what hangfire does, ran as follows and it gives an out of memory exception.

svc.RunTasks(context); // works

Task.Factory.StartNew(() => svc.RunTasks(context)); // out of memory exception.

Does that mean I cannot use hangfire without redesigning my scheduled task?