Reputation: 427
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)"}}
Upvotes: 3
Views: 4050
Reputation: 7487
You can get this error for a few different reasons, but the general underlying problem is usually that you've missed something in the setup of Ninject to be the dependency resolver for your controllers.
In my case, all of the Ninject setup was being called from the Startup.cs
class. Turns out it was fine, I was just missing the Microsoft.Owin.Host.SystemWeb
package from my project, so the Startup
code was never actually getting called. Doh!
Upvotes: 0
Reputation: 1295
I have also come across this issue. There are the Ninject packages that I have got and they are working now.
<package id="Ninject" version="3.2.0.0" targetFramework="net451" />
<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net451" />
<package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net451" />
<package id="Ninject.Web.Common.WebHost" version="3.2.0.0" targetFramework="net451" />
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net451" />
You also have to make sure that you have the start up methods in Application_Stat() in your Global.asax in the right order. Mine looks like this:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Upvotes: 0
Reputation: 126
I had the same problem after I updated my Nuget packages. Ninject.Web.WebApi version 3.2.4 introduced a breaking change that requires installing package "Ninject.Web.WebApi.WebHost" per the release notes.
After installing the mentioned package my controllers started to resolve the dependencies again.
Upvotes: 11