I have a web API 2 project that implements Ninject. It works fine if my controllers do not use Route attributes but, if I use them, the application returns the following exception: "An error occurred when trying to create a controller of type 'AccountsController'. Make sure that the controller has a parameterless public constructor."
Works fine
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
Do not work
[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
I have the following packages installed
Ninject version="3.2.0.0"
Ninject.Extensions.ContextPreservation version="3.2.0.0"
Ninject.Extensions.NamedScope version="3.2.0.0"
Ninject.Web.Common version="3.2.0.0"
Ninject.Web.Common.OwinHost version="3.2.3.0"
Ninject.Web.Common.WebHost version="3.2.3.0"
Ninject.Web.WebApi version="3.2.4.0"
Ninject.Web.WebApi.OwinHost version="3.2.4.0"
Ninject.Web.WebApi.WebHost
My NintextWebCommon class is
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Stop")]
namespace ProperdiAPI.App_Start
{
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using System;
using System.Web;
using System.Web.Http;
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);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
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;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IEntityService>().To<EntityService>().InRequestScope();
}
}
}
BaseApiController
public class BaseApiController : ApiController
{
private IEntityService entitysService;
public BaseApiController(IEntityService entityService)
{
this.entityService = entityService;
}
protected IEntityService EntitysService
{
get
{
return this.entityService;
}
}
protected IHttpActionResult GetErrorResult(IdentityResult result)
{
if (result == null)
{
return InternalServerError();
}
if (!result.Succeeded)
{
if (result.Errors != null)
{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
if (ModelState.IsValid)
{
return BadRequest();
}
return BadRequest(ModelState);
}
return null;
}
}
}
Controller
[RoutePrefix("api/accounts")]
public class AccountsController : BaseApiController
{
public AccountsController(IEntityService entityService)
:base(entityService)
{
}
[HttpGet]
//[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService .getAll();
return Ok(entities);
}
}
I've tried a lot of things, like building a custom resolver and scope, installing an old ninject version and so on, but nothing works.
Thanks a lot in advance!