无法让 DependencyResolver 与 ninject 一起使用?
我的 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>();
}
在我的全局中,我尝试这个..
protected virtual void Application_BeginRequest()
{
ContextProvider cp = new ContextProvider();
DbContext context = (DbContext)DependencyResolver.Current.GetService<IContext>();
cp.SetCurrent(context);
}
编辑:这里是完整的引导程序...但它是 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>();
}
}
但它返回 NULL ?为什么 ?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 NuGet 上找到的 Ninject.MVC3 并阅读文档 https://github.com/ ninject/ninject.web.mvc/wiki/MVC3
Use Ninject.MVC3 found on NuGet and read the documentation https://github.com/ninject/ninject.web.mvc/wiki/MVC3