1
votes

I'm making an application using the MVC framework in C#. We want to make a repository 'uitlenerRepository' which contains all of our users.

I bound the repository using Ninject in NinjectWebCommon in the RegisterServices class:

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();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            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)
    {

        kernel.Bind<IMateriaalRepository>().To<MateriaalRepository>().InRequestScope();
        kernel.Bind<IReservatieRepository>().To<ReservatieRepository>().InRequestScope();
        kernel.Bind<IUitlenerRepository>().To<UitlenerRepository>();
        kernel.Bind<UitleenAppContext>().ToSelf().InRequestScope();
    }        
}

As you can see I tried the same method with other repositories, which were successfull. The only difference between the other repositories is that I make a new one in the predefined class 'AccountController'. Because in that class, I'm able to easily add new users. In this class I made an empty constructor and added it as a third parameter.

public class AccountController : Controller
    {
        private ApplicationSignInManager _signInManager;
        private ApplicationUserManager _userManager;
        private IUitlenerRepository uitlenerRepository;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IUitlenerRepository uitlenerRepository)
    {
        UserManager = userManager;
        SignInManager = signInManager;
        this.uitlenerRepository = uitlenerRepository;
    }

We can't add new users to the repository. When debugging, I found out that the value of the repository is null, inidcating that the binding is not working properly.

Picture of debugging

Does anyone know how to resolve this problem?

1
Your controller has multiple constructors: this is an anti-pattern.Steven

1 Answers

0
votes

The AccountController() ctor is being used, that's why it's null. Ninject will choose the constructor with the most parameters it can resolve. I suspect that either ApplicationUserManager or ApplicationSignInManager need a binding. I suggest you remove the AccountController() ctor - the one without any parameters - then ninject will throw an exception and tell you exactly why it can't use the other ctor.