0

I have the following two classes. The ninject web common:

 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); GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(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<ITest>().To<Test>(); } } 

My controller class:

 public class AllCarPartController : MainController<AllCarPart, AllCarPartViewModel, E1Entities> { private ITest _test; public AllCarPartController() { } public AllCarPartController(IMemoryCache memoryCache , ITest test) : base(memoryCache) { _test = test; } } public interface ITest { void TestMe(); } public class Test : ITest { public void TestMe() { } } 

When I call the controller, the parameter less constructor is being called and the other constructor with the interfaces is not called, so my instance of ITest is always null.

What is wrong?

3
  • What happens when you remove the parameterless constructor?
    – Steven
    CommentedMar 25, 2023 at 11:34
  • @Steven An error is thrown, since the controller cannot be initialized. I figured out that if I remove the IMemoryCache it works, because it is not being registered in the RegisterServices().CommentedMar 25, 2023 at 13:04
  • Prevent adding multiple constructors to classes that are created by your DI Container. For more info, see this.
    – Steven
    CommentedMar 25, 2023 at 13:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.