I'm getting this error when loading my controller.
An error occurred when trying to create a controller of type 'MembersController'. Make sure that the controller has a parameterless public constructor.
I have added
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
to the NinjectWebCommon class after registering the bindings.
And I have registered the relevant services. see EDIT
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IMembersService, MembersService>();
kernel.Bind<MemberContext, MemberContext>();
}
Here are the ninject packages I have added -
<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" />
<package id="Ninject.WebApi.DependencyResolver" version="0.1.4758.24814" targetFramework="net451" />
What am I missing?
EDIT
Thanks to my conversation with Jerrod Horton (below) I realized I doing the binding incorrectly.
Should have been
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IMembersService>().To<MembersService>();
kernel.Bind<MemberContext>().To<MemberContext>();
}
Short answer:
You need to have all of these packages installed:
Missing one of these is causing the problem in most situations.
One of your dependencies (or a dependency of a dependency) is not fully registered with the IoC container. When web api attempts to resolve the controller if it does not have every single dependency it needs then it will fail and If your IoC container is trying to resolve ProductRepository but ProductRepository requires in its constructor ISqlConnectionProvider then you must register ISqlConnectionProvider with your IoC container as well.
I would start at the controller. Look at all input parameters. Make sure each are registered with your IoC container. Then go through each of those classes and make sure any dependencies within those are registered as well (and so on and so forth).
Here is the code that I have that works for Ninject. This code exists in the App_Start folder in a file called NinjectWebCommon.cs
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourProject.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(YourProject.App_Start.NinjectWebCommon), "Stop")]
namespace YourProject.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
}
}
}
packages.config Ninject snippet
<package id="Ninject" version="3.2.2.0" targetFramework="net45" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net45" />
<package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net45" />
<package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net45" />
<package id="Ninject.Web.WebApi.WebHost" version="3.2.4.0" targetFramework="net45" />
packages.config web api and asp.net snippet
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With