0

I have been using Ninject with ASP.NET Web API for about a month now and have had a generally good experience. However, I have a recurring issue for which I have yet to find any satisfactory solution.

Anytime Ninject has a problem instantiating any object in the dependency chain if returns control to the ASP.NET resolver. And, of course the ASP.NET complains that there is no parameterless constructor.

Is there any way to get the information from Ninject about what the underlying failure was, or even just which object it could not create.

I thought there may me a way to log the internal behavior, but cannot find any information.

Thanks

Jim Reineri
  • 2,830
  • 3
  • 31
  • 32

2 Answers2

1

I don't know too much about ASP.Net but i know a thing or two about ninject. So if i gather correctly your issue is, that whenever ninject can't resolve a request, the control is given back to the framework who then tries to instanciate the type - and fails with an exception stating that the type to resolve is missing a parameterless constructor. This is unsatisfactorily since it's hard to figure out which binding is incorrect / failing.

To gather more information i suggest that you rewrite the glue code which passes resolution request on to ninject. Instead of the IResolutionRoot.TryGet<> method use the IResolutionRoot.TryGetAndThrowOnInvalidBinding<>() method. This method will throw an exception in case there is a binding but ninject can't resolve it. ninject's IResolutionRoot.TryGet<> swallows these kinds of issues.

Now if there is no explicit binding, ninject will still return null and the ASp.Net framework might still throw an exception stating that there is no missing parameterless constructor. I suggest there should be information about the type it tried to resolve on the exception. If that is somehow not the case, you change the ninject-glue-code to log all requests that it could not resolve. Then you should create explicit bindings for all of these so you can narrow it down step-by-step.

EDIT: In case you plan to define all bindings explicitly / you already have done so, go for IResolutionRoot.Get<>() - without the try. This will always lead to a NinjectException whenever it can't resolve the type. The exception will tell you more on why it happened.

Also see Are controller factories neccessary when using Ninject in ASP.NET mvc 4 on how to write your own glue-code.

Community
  • 1
  • 1
BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68
0

This happens because you didn't register your MVC controllers explicitly in Ninject. For more information, read this answer. Although that answer is related to Web API, MVC behaves the same in this respect.

Community
  • 1
  • 1
Steven
  • 166,672
  • 24
  • 332
  • 435
  • Thanks. Your answer on the other answer was interesting reading and shed some insight to how ASP.NET interacts with DI frameworks. However, explicitly registering my controller did not make Ninject give any useful information. I still just get the relatively uninformative 'parameterless constructor' exception. – Jim Reineri Jul 18 '14 at 19:15