1

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!

4
  • Could you add the controller class constructor to the question as well? Commented Apr 21, 2017 at 8:11
  • I've just added the controller class and the baseApiController I use. Thanks! Commented Apr 21, 2017 at 8:28
  • If you find this helpful stackoverflow.com/questions/24195159/… Commented Apr 21, 2017 at 9:34
  • Thanks @MukeshModhvadiya for your response. I tried it but it didn't work. Still getting the same error. Thanks a lot! Commented Apr 21, 2017 at 10:04

1 Answer 1

0

Install plugin Ninject.WebApi.DependencyResolver and add this code after call to RegisterServices(kernel);:

 System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This fixed the error and now my controllers are working! Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.