Using Ninject DI for both api and “regular” controllers in MVC4 [duplicate]

核能气质少年 提交于 2019-12-10 06:10:01

问题


Possible Duplicate:
MVC3 + Ninject - How to?

In a small mvc 4 project, I'm trying to implement dependency injection using ninject.

So far, I have it working with api controllers, but I'm not having any luck with regular controllers.

I have A NinjectResolver:

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
    {
        private readonly IKernel _kernel;

        public NinjectDependencyResolver(IKernel kernel)
            : base(kernel)
        {
            _kernel = kernel;
        }

        public IDependencyScope BeginScope()
        {
            return new NinjectDependencyScope(_kernel.BeginBlock());
        }

        public override void Dispose()
        {
            _kernel.Dispose();
        }
    }

And a NinjectScope:

public class NinjectDependencyScope : IDependencyScope
    {
        protected IResolutionRoot ResolutionRoot;

        public NinjectDependencyScope(IResolutionRoot kernel)
        {
            ResolutionRoot = kernel;
        }

        public object GetService(Type serviceType)
        {
            var request = ResolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return ResolutionRoot.Resolve(request).SingleOrDefault();
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            var request = ResolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
            return ResolutionRoot.Resolve(request).ToList();
        }

        public void Dispose()
        {
            var disposable = (IDisposable)ResolutionRoot;
            if (disposable != null) disposable.Dispose();
            ResolutionRoot = null;
        }
    }

Then I set it up in my Global.asax:

var kernel = new StandardKernel();
            kernel.Bind(typeof(IRepository)).To(typeof(Repository));

            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

Then I inject a repository into an api controller like this:

private IRepository _repository;

public TestApiController(IRepository repository)
{
    _repository = repository;
}

This works fine, but doing the same in a regular controller fails with the "No parameterless constructor defined for this object" error.

Any ideas?


回答1:


ASP.NET MVC uses a different IDependencyResolver than ASP.NET WebAPi namelly Sytem.Web.Mvc.IDependencyResolver.

So you need a new NinjectDependencyResolver implementation for the MVC Controllers (luckily the MVC and WepAPi IDependencyResolver almost has the same members with the same signatures, so it's easy to implement)

public class NinjectMvcDependencyResolver : NinjectDependencyScope, 
                                            System.Web.Mvc.IDependencyResolver
{
    private readonly IKernel _kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        _kernel = kernel;
    }
}

And then you can register it in the Global.asax with:

DependencyResolver.SetResolver(new NinjectMvcDependencyResolver(kernel));


来源:https://stackoverflow.com/questions/12212561/using-ninject-di-for-both-api-and-regular-controllers-in-mvc4

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