ASP.NET Web API binding with ninject

后端 未结 11 940
Happy的楠姐
Happy的楠姐 2020-12-12 22:46

I have just installed the mvc4 rc update and I am trying to build an api application with little luck.

I am using ninject but cant get my controllers to load. I keep

相关标签:
11条回答
  • 2020-12-12 23:10

    For someone landing here while searching '...does not have a default constructor' looking for an easy way to debug silently failing configurations:

    1. Remove constructor-injected objects until the constructor can be invoked
    2. For each of the previously injected, now uninitialized objects, invoke using:

    ServiceLocator.Current.GetInstance<[OBJ-TYPE]>()

    The offending object will cause an ActivationException with a descriptive Message. You'll have something to go for.

    Remember to remove call to ServiceLocator post-fix. Ie. this is not a recommendation to use the service locator anti pattern outside debugging.

    0 讨论(0)
  • 2020-12-12 23:14

    I asked Brad Wilson about this and it has changed in MVC4 RC.

    GlobalConfiguration.Configuration.ServiceResolver has been moved to GlobalConfiguration.Configuration.DependencyResolver

    Use this implementation to create a Ninject DependencyResolver for your Web Api: https://gist.github.com/2417226

    In NinjectWebCommon.cs:

    // Register Dependencies
    RegisterServices(kernel);
    
    // Set Web API Resolver
    GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
    
    0 讨论(0)
  • 2020-12-12 23:15

    just install Ninject.MvcXXX package, where XXX - version of MVC...

    0 讨论(0)
  • 2020-12-12 23:15

    I had this error message, too, but it just turned out that one of my interfaces weren't actually implemented by any classes (I forgot to add it to the class declaration).

    0 讨论(0)
  • 2020-12-12 23:17

    You can install the NuGet package WebApiContrib.IoC.Ninject and add the following line of code to NinjectWebCommon.cs

    GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);
    
    0 讨论(0)
提交回复
热议问题