Search code examples
c#dependency-injectionninjectasp.net-mvc-5

MVC5 and Ninject:Parameterless constructor error


In MVC 5 we get this account controller that looks like this.

public class AccountController : Controller
        {

            public AccountController()
                : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DatePickerDbContext())))
            {

            }

            public AccountController(UserManager<ApplicationUser> userManager)
            {
                UserManager = userManager;
            }
        }

I installed Ninject to handle my dependency injection. I have two repositories, that I want to use in my AccountController and the code now looks like this

    public class AccountController : Controller
    {
              private readonly ILicenserepository _licenserepository;
              private readonly IUserRepository _userRepository;    

        public AccountController(ILicenserepository licenserepository, IUserRepository userRepository)
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DatePickerDbContext())))
        {
            _licenserepository = licenserepository;
            _userRepository = userRepository;
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }
    }

in ninject.web.common this is what i've done

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind(typeof (ILicenserepository)).To(typeof (Licenserepository));
    kernel.Bind(typeof (IUserRepository)).To(typeof (UserRepository));
}    

but when I run the application i get the error on browser that says no parametereless constructor found. And if i create parameter less constructor my repositories doesn't get instantiated. So wherever i called the method from repository the value is null. How can I solve this problem? Or make MVC or Ninject know which constructor to call?


Solution

  • You need a DependencyResolver registered in Global.asax.cs

    Registration would look something like this:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
    
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        RegisterDependencyResolver();
    }
    
    private void RegisterDependencyResolver()
    {
        var kernel = new StandardKernel();
    
        // you may need to configure your container here?
        RegisterServices(kernel);
    
        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
    }
    

    And the DependencyResolver

    public class NinjectDependencyResolver : IDependencyResolver
    {
        private readonly IKernel kernel;
    
        public NinjectDependencyResolver(IKernel kernel)
        {
            this.kernel = kernel;
        }
    
        public object GetService(Type serviceType)
        {
            return this.kernel.TryGet(serviceType);
        }
    
        public IEnumerable<object> GetServices(Type serviceType)
        {
            try
            {
                return this.kernel.GetAll(serviceType);
            }
            catch (Exception)
            {
                return new List<object>();
            }
        }
    }