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

5
  • 1
    Possible duplicate of Parameterless constructor error with Ninject bindings in .NET Web Api 2.1 Commented Oct 27, 2017 at 17:33
  • MVC5, Web API 2 and Ninject may also be helpful if you are putting MVC and Web API in the same project. Commented Oct 27, 2017 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." Commented Oct 27, 2017 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 Commented Oct 28, 2017 at 20:10
  • This is not a duplicate of the related question. Commented Oct 31, 2017 at 15:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.