I have used NuGet to install the latest version of Ninject (v2.2.1.4).
I have then created my own NinjectDependencyResolver (credit to Adam Freeman & Steve Sanderson):
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver()
{
kernel = new StandardKernel();
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType) {
return kernel.GetAll(serviceType);
}
public IBindingToSyntax<T> Bind<T>()
{
return kernel.Bind<T>();
}
public IKernel Kernel
{
get { return kernel; }
}
private void AddBindings()
{
kernel.Bind<ITitleRepository>().To<TitleRepository>();
kernel.Bind<IDayRepository>().To<DayRepository>();
kernel.Bind<IMonthRepository>().To<MonthRepository>();
}
}
And then registered the dependency resolver in the global.asax applcation startup:
protected void Application_Start()
{
//...other code
DependencyResolver.SetResolver(new NinjectDependencyResolver());
}
I then have the following line in my code:
ITitleRepository titleRepository = (ITitleRepository)DependencyResolver.Current.GetService(typeof(ITitleRepository));
If I run the code through in debug mode it appears to work correctly, however, if I step into this code (line by line) then when it runs the kernel.TryGet(serviceType) the following error occurs:
No Source Available
Hopefully the image will be visible?
Does anyone have any idea why this may be occuring?