1

This is my first time using Ninject and I'm facing some problems with my MVC 4 application.

I created a class called IocConfig at App_Start folder in my Presentation Layer.

IocConfig class

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

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.
Community
  • 1
  • 1
Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118
  • Why do you want to do this on your own? Why don't you use the NuGet package Ninject.MVC4? This should do all you want (if I understand what you are trying to do :)) – Kevin Brechbühl Aug 20 '14 at 17:32
  • @KevinBrechbühl I've installed the Ninject with Nuget package, but I got the error `No parameterless constructor defined for this object.` Researching about this error I found this thread that I mentioned and seems that the solution ought to construct a new Factory – Lucas_Santos Aug 20 '14 at 17:37
  • 1
    I have exactly the same `NinjectWebCommon.cs` file as the question mentioned within the updated question, and this works very well. How does your `NinjectWebCommon` looks like? The only thing you need to do is to install `Ninject` and `Ninject.MVC4` from NuGet and run. – Kevin Brechbühl Aug 20 '14 at 17:41

1 Answers1

1

You should simply install ´Ninject.MVC4´ from NuGet. Then you get a class NinjectWebCommon.cs in App_Start. In the method RegisterServices you can place your binding configurations.

A short and nice introduction on how to use Ninject with ASP.NET MVC can be found here.

Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
  • So my IocConfig file can be deleted to use just the `NinjectWebCommon.cs` ? Great video and great tip, I read some articles and this solution using a new class called `IocConfig` was something that I "learnt" with those articles. – Lucas_Santos Aug 20 '14 at 17:56
  • @Lucas_Santos yes this can be deleted. – Kevin Brechbühl Aug 20 '14 at 18:01