It's probably a duplicate, but no somution have worked for me; so I post the question one more time.
I have a web project using both MVC5 and WebApi2. I've put constructor DI with Ninject on those controllers, but when I try to reach a web api controller I have this exception :
Make sure that the controller has a parameterless public constructor.
I have this in my NinjectWebCommon.CreateKernel()
method :
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(kernel);
System.Web.Mvc.DependencyResolver.SetResolver(new Ninject.Web.Mvc.NinjectDependencyResolver(kernel));
return kernel;
Here is the controller relevant content :
private MyDependency dependency;
public MyController(MyDependency injectedDependency)
{
this.dependency = injectedDependency;
}
And the relevant binding :
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<MyDependency>().ToSelf().InSingletonScope();
}
I tried creating a custom resolver, following this solution, but I still have the exception. I also tried using Unity, and I have the same problem.
What else can I try ?