简体   繁体   English

在MVC 4项目中使用Ninject创建IKernel

[英]Create IKernel with Ninject in MVC 4 project

This is my first time using Ninject and I'm facing some problems with my MVC 4 application. 这是我第一次使用Ninject,我的MVC 4应用程序遇到了一些问题。

I created a class called IocConfig at App_Start folder in my Presentation Layer. 我在表示层的App_Start文件夹中创建了一个名为IocConfig的类。

IocConfig class IocConfig类

public class IocConfig
{
    public static void ConfigurarDependencias()
    {
        IKernel kernel = new StandardKernel();
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(kernel));

        kernel.Bind<IReceitaRepository>().To<SqlReceitaRepository>();

        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
    }
}

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory(IKernel kernel)
    {
        ninjectKernel = kernel;
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return (controllerType == null) ? null : (IController)ninjectKernel.Get(controllerType);
    }
}

public class NinjectDependencyResolver : IDependencyResolver 
{
    private readonly IResolutionRoot _resolutionRoot;

    public NinjectDependencyResolver(IResolutionRoot kernel) 
    {
        _resolutionRoot = kernel;
    }

    public object GetService(Type serviceType)
    {
        return _resolutionRoot.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _resolutionRoot.GetAll(serviceType);
    }
}

And after read the thread ASP.NET MVC 4 + Nunject MVC 3 , I'm trying to implement the same solution proposed by @JereJones, but I got the following error at the line below 在阅读了线程ASP.NET MVC 4 + Nunject MVC 3之后 ,我试图实现@JereJones提出的相同解决方案,但是在下面的行中出现了以下错误

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
    return (controllerType == null) ? null : (IController)ninjectKernel.Get(controllerType);
}

Error 错误

Error activating ISession
No matching bindings are available, and the type is not self-bindable.
Activation path:
 3) Injection of dependency ISession into parameter session of constructor of type SqlReceitaRepository
 2) Injection of dependency IReceitaRepository into parameter repositorio of constructor of type HomeController
 1) Request for HomeController

Suggestions:
 1) Ensure that you have defined a binding for ISession.
 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
 3) Ensure you have not accidentally created more than one kernel.
 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
 5) If you are using automatic module loading, ensure the search path and filters are correct.

You should simply install ´Ninject.MVC4´ from NuGet. 您只需从NuGet安装“ Ninject.MVC4”即可。 Then you get a class NinjectWebCommon.cs in App_Start . 然后你会得到一个类NinjectWebCommon.csApp_Start In the method RegisterServices you can place your binding configurations. 在方法RegisterServices ,可以放置绑定配置。

A short and nice introduction on how to use Ninject with ASP.NET MVC can be found here . 这里可以找到有关如何将Ninject与ASP.NET MVC一起使用的简短介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM