Bootstrapper不加载我的NinjectModules
本文关键字:NinjectModules 我的 加载 Bootstrapper | 更新日期: 2023-09-27 18:13:32
我正在尝试使用Bootstrapper与Ninject。我已经安装了Ninject.MVC3
来初始化我的Ninject容器从nuget,我已经创建了一个测试模块,像这样:
public class TestNinjectModule : NinjectModule
{
public override void Load()
{
Bind<DBEntities>().ToSelf().InRequestScope();
Bind(typeof(IGenericRepository<>))
.To(typeof(GenericRepository<>)).InRequestScope();
Bind<ISystemRepository>().To<SystemRepository>().InRequestScope();
Bind<IUnitOfWork>().To<UnitOfWork>();
}
}
我还将Start()
调用包括在我的Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Bootstrapper.With.Ninject().Start();
Bootstrapper.With.AutoMapper().Start();
}
}
然而,当调用具有依赖关系的控制器时,我仍然从Ninject获得ActivationException
,这表明我的TestNinjectModule.Load()
方法未被调用。怎么解呢?
异常信息:
激活IUnitOfWork错误没有匹配的绑定可用,并且类型不能自绑定。激活路径:2)将依赖项IUnitOfWork注入到类型> TreasurySystemController的构造函数的参数unitOfWork中1)请求SystemController
建议:1)确保你已经为IUnitOfWork定义了绑定。2)如果绑定是在一个模块中定义的,确保该模块已经加载到内核中。3)确保您没有意外创建多个内核。4)如果使用构造函数参数,请确保参数名称与>constructor参数名称匹配。5)如果你使用自动模块加载,确保搜索路径和过滤器是正确的。
堆栈跟踪:
在Ninject.KernelBase。解决(IRequest请求)在c:'Projects'Ninject' Ninject' src'Ninject'KernelBase.cs:行359在Ninject.Planning.Targets.Target
1.GetValue(Type service, IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 197 at Ninject.Planning.Targets.Target
c:'Projects'Ninject' Ninject' src'Ninject'Planning'Targets'Target.cs中的ResolveWithin(IContext parent):第165行在Ninject.Activation.Providers.StandardProvider。GetValue(IContext上下文,ittarget目标)在c:'Projects'Ninject' Ninject' src'Ninject'Activation'Providers'StandardProvider.cs:第114行在Ninject.Activation.Providers.StandardProvider灵活;> c_ DisplayClass4。b_2(ittarget target)在c:'Projects'Ninject' Ninject' src'Ninject'Activation'Providers'StandardProvider.cs:第96行在System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Buffer
1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 source)在Ninject.Activation.Providers.StandardProvider。创建(IContext上下文)在c:'Projects'Ninject' Ninject' src'Ninject'Activation'Providers'StandardProvider.cs:第96行在c:'Projects'Ninject' Ninject' src'Ninject'Activation'Context.cs:line 157在Ninject.KernelBase灵活;> c_ DisplayClass10。b_c(绑定绑定)在c:'Projects'Ninject' Ninject' src'Ninject'KernelBase.cs:行386在system_linq . enumerable . whereselectenumerableiterator2.MoveNext() at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable
1源)在Ninject.Web.Mvc.NinjectDependencyResolver。GetService(类型serviceType)在c:'Projects'Ninject' Ninject. web.mvc'mvc3'src'Ninject. web.mvc' NinjectDependencyResolver.cs:第56行在System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator。Create(RequestContext RequestContext, Type controllerType)
我找到了一个解决方案。通过将NinjectWebCommon
的实现更改为:
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static IKernel Kernel { get; private set; } // Expose the kernel through a property
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
return Kernel = kernel;
}
}
然后在Global.asax.cs
:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Bootstrapper.With.AutoMapper().Start();
Bootstrapper.With.Ninject()
.WithContainer(NinjectWebCommon.Kernel) // Use the kernel inside NinjectWebCommon instead of creating a new one
.Start();
}
}
那么所有从NinjectModule
派生的类都将调用它们的Load()
方法