Can't get DependencyResolver to work with ninject?
I have this code in my bootstap of Ninject :
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
//TODO: Enregistrer les services ici.. pour ninject!
kernel.Bind<IContext>().To<MySampleContext>();
kernel.Bind<ISampleService>().To<SampleService>();
}
In my global i try this..
protected virtual void Application_BeginRequest()
{
ContextProvider cp = new ContextProvider();
DbContext context = (DbContext)DependencyResolver.Current.GetService<IContext>();
cp.SetCurrent(context);
}
EDIT: Here the full bootstrapper... but its the basic one that come with ninjectt.
public static class NinjectMVC3
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
bootstrapper.Initialize(CreateKernel);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
//TODO: Enregistrer les services ici.. pour ninject!
kernel.Bind开发者_开发知识库<IContext>().To<MySampleContext>();
kernel.Bind<ISampleService>().To<SampleService>();
}
}
but it return NULL ? why ?
Use Ninject.MVC3 found on NuGet and read the documentation https://github.com/ninject/ninject.web.mvc/wiki/MVC3
精彩评论