Ninject giving NullReferenceException
Posted
by Iceman
on Stack Overflow
See other posts from Stack Overflow
or by Iceman
Published on 2010-04-04T14:28:58Z
Indexed on
2010/04/04
14:33 UTC
Read the original article
Hit count: 297
asp.net-mvc-2
|ninject-2
I'm using asp.net MVC 2 and Ninject 2.
The setup is very simple. Controller calls service that calls repository.
In my controller I use inject to instantiate the service classes with no problem. But the service classes don't instantiate the repositories, giving me NullReferenceException.
public class BaseController : Controller
{
[Inject]
public IRoundService roundService { get; set; }
}
This works. But then this does not...
public class BaseService
{
[Inject]
public IRoundRepository roundRepository { get; set; }
}
Giving a NullReferenceException, when I try to use the roundRepository in my RoundService class.
IList<Round> rounds = roundRepository.GetRounds( );
Module classes... public class ServiceModule : NinjectModule { public override void Load( ) { Bind( ).To( ).InRequestScope( ); } }
public class RepositoryModule : NinjectModule
{
public override void Load( )
{
Bind<IRoundRepository>( ).To<RoundRepository>( ).InRequestScope( );
}
}
In global.axax.cs
protected override IKernel CreateKernel( )
{
return new StandardKernel( new ServiceModule( ),
new RepositoryModule( ) );
}
© Stack Overflow or respective owner