Mike Flynn
Mike Flynn

Reputation: 24325

Ninject Injection Issues in ASP.NET MVC 3

I have three projects that will use DI. My project is an ASP.NET MVC 3 site. I installed the ninject MVC package from nuget which added my bootstrapper. I registered my binding.

NinjectMVC3.cs

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IUserService>().To<UserService>();
        }   

I have a separate project that contains the UserService and IUserService. In that project I also have a singleton factory class to create my services.

public class ServiceFactory : BaseFactory<ServiceFactory>
    {
        [Inject]
        public IUserService UserService { get; set; }
    }

Is there a reason why the above is always null when I call ServiceFactory.Instance.UserService?

Upvotes: 0

Views: 600

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

Guessing: Most likely the factory isn't created by Ninject.

But as you shouldn't use the Singleton pattern anyway when doing Dependency Injection this should be easy to solve.

  1. Pass IUserService to the constructor of any object that needs to know it.
  2. Bind it as Singleton kernel.Bind<IUserService>().To<UserService>().InSingletonScope();

Upvotes: 2

Related Questions