Search code examples
dependency-injectionninjectasp.net-mvc-5hangfirehangfire.ninject

Problems integrating Hangfire with Ninject configuration InRequestScope


I am integrating Hangfire into an application (MVC5 4.6) in order to schedule a daily synchronisation task that is normally triggered by an admin user every evening.

The problem is, I can't seem to get Hangfire to play nice with my current Ninject setup. It won't just run a function itself and I needed to install Hangfire.Ninject package and register the kernel in Startup.cs.

The problem is, I have dependency A which contains dependency B, but dependency B also contains dependency A (cyclical).

As these are declared inRequestScope in the Ninject kernel set-up, this hasn't caused any issues, but this particular item is used so much throughout the application it would be far too painful to try and remove the cyclical dependency, but for some reason Hangfire won't accept it.

Are there any alternatives to removing the cyclical dependency?


Solution

  • I still haven't quite avoided the issue completely, but for Hangfire purposes I decided to register a different Ninject Kernel and could customise what I needed precisely.

    Most dependencies work now using .InBackgroundJobScope() rather than .InRequestScope() (from the Hangfire Ninject nuget package), but oddly a few others (which luckily I don't need) still give me the cyclical dependency issue.