I was watching a tutorial from Pluralsight when I came across this problem: Searched this site and found (Simple Injector Unable to Inject Dependencies in Web API Controllers, Make sure that the controller has a parameterless public constructor error ) different solutions but they didn't work either. Having the same problem using WebApi and Ninject, nothing was done by the Author while writing code, don't know why I am facing this, would appreciate if someone explains the reason along with the solution (what to write and where to write). Here's the code:
//NinjectWebCommon.cs
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IRepository>().To<Repository>().InRequestScope(); //for making singleton
kernel.Bind<WebAPIandEFContext>().To<WebAPIandEFContext>().InRequestScope();
}
//Order Controller
public class OrderController : ApiController
{
private IRepository _repo;
public OrderController(IRepository repo)
{
_repo = repo;
}
//remaining code....
ERROR description
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"An error
occurred when trying to create a controller of type 'OrderController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"Type 'WebAPIandEF.Controllers.OrderController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}