2

I've read every Stack Overflow question I can find but to no avail. When I installed the Ninject packages through NuGet, the NinjectWebCommon.cs class was not installed, so I coded up my own (below). I installed Ninject, Ninject.WebCommon, Ninject.Webcommon.Webhost, Ninject.WebApi, Ninject.WebApi.DependencyResolver. However, I am getting the well-known message in my controller that I must have a parameterless controller, which of course I don't want since I want to require DI.

Here's the Ninject class I created:

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();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
        return kernel;
    }


    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel)
    {
        //kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Bind<IUnitsSL>().To<UnitsSL>();
        kernel.Bind<IForecastLogic>().To<ForecastLogic>();
        kernel.Bind<IForecastRepositoryAsync>().To<ForecastRepositoryAsync>();
    }
}

Here is the controller

namespace ForecastApi.Controllers
{
  public class ForecastController : ApiController
{
    private IUnitsSL _serviceLayer { get; set; }
    private readonly IForecastLogic _forecastLogic;
    public ForecastController(IForecastLogic forecastLogic)
    {
        _forecastLogic = forecastLogic;
        _serviceLayer = new UnitsSL();
    }
[Route("projection")]
    [HttpPost]
    public async Task<IHttpActionResult> GetDemandForecast([FromBody] 
ProjectionRequestModel model)
    {
        var retVal = await _forecastLogic.GetDemandForecastData(model);
        return Ok(retVal);
    }
}

The exact wording of the error message when I test this through Postman is: An error occurred when trying to create a controller of type 'ForecastController'. Make sure that the controller has a parameterless public constructor."

If it helps, I am mocking my unit tests for the business calls and they are working with the Ninject library and Moq. It is the constructor that seems to fail.

TIA

Ron
  • 867
  • 11
  • 26
  • 1
    Possible duplicate of [Parameterless constructor error with Ninject bindings in .NET Web Api 2.1](https://stackoverflow.com/questions/22768721/parameterless-constructor-error-with-ninject-bindings-in-net-web-api-2-1) – NightOwl888 Oct 27 '17 at 17:33
  • [MVC5, Web API 2 and Ninject](https://stackoverflow.com/q/20595472/181087) may also be helpful if you are putting MVC and Web API in the same project. – NightOwl888 Oct 27 '17 at 17:34
  • Prior to asking this question, I reviewed the other question and the answers there did not help. I do have the Mvc5 Ninject package, and the Ninject package is required for any of the others, so I have no idea what you mean by "it may be helpful." – Ron Oct 27 '17 at 17:50
  • The error messages are often misleading. The problem is probably in the ForeCastLogic chain of instantiation. Try adding your dependencies one by one and see when the resolution fails – jbl Oct 28 '17 at 20:10
  • This is not a duplicate of the related question. – Ron Oct 31 '17 at 15:43

1 Answers1

0

For whatever reason, uninstalling Ninject entirely and reinstalling it (this is version 3.3) solved the problem. Leaving this here in the event that anyone else has a similar issue. I had originally installed

  • Ninject
  • Ninject.Web.Common
  • Ninject.Web.Common.WebHost
  • Ninject.Web.WebApi
  • Ninject.Web.WebApi.WebHost

When I uninstalled all of these packages, then re-installed them v3.3 again, the NinjectWebCommon.cs file was added and all worked ok. I can only assume that there was something missing when I rolled my own common file to make up for Ninject not installing it the first go around.

Ron
  • 867
  • 11
  • 26