我在我的 MVC 4 项目中使用 Unity 为我的 IoC 进行了此配置。
container.RegisterType<IDbContext, ConstructionRepositoryContext>("ConstructionRepositoryContext", new TransientLifetimeManager());
container.RegisterType<IDbContext, GeneralRepositoryContext>("GeneralRepositoryContext", new TransientLifetimeManager());
container.RegisterType<IUnitOfWork, UnitOfWork>(
"ConstructionUnitOfWork",
new InjectionConstructor(container.Resolve<IDbContext>("ConstructionRepositoryContext")));
container.RegisterType<IUnitOfWork, UnitOfWork>(
"GeneralUnitOfWork",
new InjectionConstructor(container.Resolve<IDbContext>("GeneralRepositoryContext")));
当我更改或其他内容TransientLifetimeManager
时PerRequestLifetimeManager
,会发生以下异常:
(第 101 行错误)
Operation is not valid due to the current state of the object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Line 99: new InjectionConstructor(container.Resolve<IDbContext>("ConstructionRepositoryContext")));
Line 100:
Line 101: container.RegisterType<IUnitOfWork, UnitOfWork>(
Line 102: "GeneralUnitOfWork",
Line 103: new InjectionConstructor(container.Resolve<IDbContext>("GeneralRepositoryContext")));