Parameterless constructor missing - NInject Dependency Injection

183 Views Asked by At

I have tried to use Ninject 3 to inject "RuleChecker" into my controller, but it doesnt work.

Error "Make sure that the controller has a parameterless public constructor."

I have read most of the topics with similar issues but could not find a solution for my case.

It should be simple, but I can't figure out how to make it work. Thanks

public class CarController : ApiController
{
    private readonly IRule rule;

    public CarController(IRule rule)
    {
        this.rule = rule;
    }
}



public class RuleChecker : IRule
{

    private ICheckCar CarData { get; }
    private IRuleSet RuleSet { get; } 
    private IFactory Factory { get; }    

    public RuleChecker(ICheckCar carData, IRuleSet ruleset,
                               IFactory factory)
    {
              CareData= cardata;
              RuleSet = ruleSet;
              Factory = factory;
    }
}

Few examples what I have tried but it doesn't work

    private static void RegisterServices(IKernel kernel)
    {
           kernel. Bind<IRule>().To<RuleChecker>();
           
           //OR

           kernel.Bind<IRule>()
               .ToConstructor(
                    ctorArg => new RuleChecker(
                        ctorArg.Inject<ICarData>(),
                        ctorArg.Inject<IRuleSet>(),
                        ctorArg.Inject<IFactory>()
                    )
                );
   }

Ninject:

private static IKernel CreateKernel()
        {
            var kernel = default(IKernel);
            try
            {
                kernel = CommonDependencyRegistration.Setup();
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
                RegisterServices(kernel);
                GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
                return kernel;
            }
            catch
            {
                kernel?.Dispose();
                throw;
            }
        }

Global.asax

 public class WebApiApplication : HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            var config = GlobalConfiguration.Configuration;
            config.Formatters.JsonFormatter.SerializerSettings.Converters.Add
                (new Newtonsoft.Json.Converters.StringEnumConverter());
        }
    }
2

There are 2 best solutions below

3
AVTUNEY On

You can use ToMethod() to create an instance of RuleChecker and inject dependencies. This should do it.

Bind<IRule>().ToMethod(context =>
    new RuleChecker(
        context.Kernel.Get<ICheckCar>(),
        context.Kernel.Get<IRuleSet>(),
        context.Kernel.Get<IFactory>()
    )
);
1
Jürgen Röhr On

ASP.Net needs the default ctor.
You need to have the controller resolved by NInject (or any other di container). Only then you can inject into controllers. s.https://stackoverflow.com/questions/14016727/how-to-use-ninject-with-asp-net-web-api