0
votes

I am trying to integrate Ninject in my WebAPI 2 project but I am getting following error:

{
    "message": "An error has occurred.",
    "exceptionMessage": "An error occurred when trying to create a controller of type 'BrandController'. 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": {
        "message": "An error has occurred.",
        "exceptionMessage": "Type 'ADAS.GoTango.WebApi.Controllers.BrandController' 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)"
    }
}

My package config is:

<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" />
<package id="Ninject.WebApi.DependencyResolver" version="0.1.4758.24814" targetFramework="net45" />

My code :

public class BrandController : BaseApiController
{
    readonly BrandsBusiness _brandsBusiness;

    public BrandController(BrandsBusiness brandsBusiness)
    {
        _brandsBusiness = brandsBusiness;
    }

    //public BrandController()
    //{
    //    _brandsBusiness = new BrandsBusiness(new BrandEfStore());
    //}

    public IHttpActionResult Get()
    {
        try
        {
            var allActiveBrands = _brandsBusiness.GetAllActiveBrands();
            return Ok(allActiveBrands);
        }
        catch (Exception exception)
        {
            Logger.Error(exception);
            return InternalServerError();
        }
    }

}

and NinjectWebCommon.cs file is

/// <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>();
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        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)
{
    var configuration = new HttpConfiguration();
    kernel.Bind<DefaultModelValidatorProviders>().ToConstant(new DefaultModelValidatorProviders(configuration.Services.GetServices(typeof(ModelValidatorProvider)).Cast<ModelValidatorProvider>()));
    kernel.Bind<BrandsBusiness>().ToSelf().InRequestScope();
    kernel.Bind<IBrandManagement>().To<BrandEfStore>().InRequestScope();
}    

I have alredy tried :

Parameterless constructor error with Ninject bindings in .NET Web Api 2.1

Ninject.ActivationException thrown only on first web request (WebAPI 2, OWIN 3, Ninject 3)

but none of them worked.

1

1 Answers

0
votes

While I don't know Ninject, I'd imagine that you'll need to make sure that the CreateKernel method is called. You'd normally add an Application_Start method in your global.asax.

You may need to make the CreateKernel method internal or public in order to be able to call if from there.