Ninject not binding object in BaseController in MVC3

荒凉一梦 提交于 2019-12-14 02:52:14

问题


I just upgraded my MVC2 project to MVC3 and used the NuGet library package reference to install ninject. This created an appstart class and i used the following code to inject my IMembershipService class.

public static void RegisterServices(IKernel kernel) {
        kernel.Bind<IMembershipService>().To<AccountMembershipService>();
    }

This works great with my HomeController, for example.

public class HomeController : Controller
{
    public IMembershipService MembershipService { get; set; }

    public HomeController() : this(null) { }

    public HomeController(IMembershipService service)
    {
        MembershipService = service;
    }

HOWEVER, I am using a BaseController. Nearly the same code in the base class no longer works.

public class BaseController : Controller
{
    public IMembershipService MembershipService { get; set; }
    public UserService UserService { get; set; }

    public BaseController() : this(null, null) { }

    public BaseController(IMembershipService service, UserService userService)
    {
        MembershipService = service;
        UserService = userService ?? new UserService();
    }

If I break in the constructor of the base controller, service is just NULL. I have never used Ninject for IOC so perhaps the answer is obvious, but why will it not inject my AccountMembershipController in the base class like I want it to? I don't see what is different, although i realize the extra level of inheritance may be messing with Ninject somehow.


回答1:


I ran into this same problem myself. Assuming your code looks like this:

public HomeController : BaseController
{

}

public BaseController : Controller
{
    public IMembershipService MembershipService { get; set; }

    public MembershipService() { }

    public MembershipService(IMembershipService service)
    {
        MembershipService = service;
    }
}

For some reason, Ninject thinks that HomeController only has one constructor, the default parameterless one. When you put everything in HomeController, it can find the injectable constructor, but factor it out into a base class and for some reason it won't look in the base class to see if there are any overloaded constructors. There are two fixes for this:

  1. Remove the default constructor. This is my preferred solution because it forces the constructor to be injected (like when you create the controller manually when unit testing), but the downside is that you have to implement the constructor in all your subclasses.
  2. Keep the default constructor, but add the [Inject] attribute to all your injectable properties:

    public BaseController : Controller
    {
        [Inject] public IMembershipService MembershipService { get;set; }
        // rest is the same
    }
    

Ninject will inject the properties correctly this way, but be aware that Ninject will call the parameterless constructor.




回答2:


Your HomeController dervices from Controller, not BaseController? Also, you have a default constructor for BaseController that sets things as null. Why do you have that at all? I'd start by getting rid of those default constructors. You shouldn't need any default constructors.



来源:https://stackoverflow.com/questions/4148788/ninject-not-binding-object-in-basecontroller-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!