0

I'm having a frustrating issue with Ninject and MVC 4.

Here is the code block:

Kernel.Bind<IUserInfo>().To<UserInfo().InRequestScope();
var userInfo = Kernel.Get<IUserInfo>();

Most of the time, this is fine, and I have a user info. Sometimes, though, I get the following error:

Error activating IUserInfo
No matching bindings are available, and the Type is not self-bindable.
Activation path:
    1) Request for IUserInfo
Suggestions:
    1) Ensure that you have defined a binding for IUserInfo.
    2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
    3) Ensure you have not accidentally created more than one kernel.      
    4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.\r\n  5) If you are using automatic module loading, ensure the search path and filters are correct.

I've pared down everything I cant think to, and am at a loss. I don't know why this would fail intermittently. Based on my admittedly limited knowledge of Ninject, there should be no way for the binding to be missing.

I see a lot of references to using the Ninject MVC Nuget packages, but the app as I inherited it does not use those, it initializes Ninject using an ActionFilter. Is this pattern just broken at its core, and somehow interfering with proper binding?

Help?

normanthesquid
  • 690
  • 1
  • 6
  • 21

2 Answers2

0

Take a look at the BindFilter option https://github.com/ninject/ninject.web.mvc/wiki/Filter-configurations

There is some sort of caching issue I believe, that makes filters behave differently to controllers. This means that the binding can fail, usually under heavy load, but unpredicatably.

ste-fu
  • 6,879
  • 3
  • 27
  • 46
0

It turns out that newer versions of Ninject need more setup for InRequestScope to work. By removing Ninject entirely, and readding references to Ninject, Ninject.Web.Common, and Ninject.Web.MVC, it added the Ninject.Web.Common.cs file that was neccessary for InRequestScope to work.

Previously, it was actually binding InTransientScope, which meant it would get garbage collected, which is non-deterministic, which explains my intermittent issues. I wish it would have thrown exceptions when i tried to bind InRequestScope, but c'est la vie.

normanthesquid
  • 690
  • 1
  • 6
  • 21