zay*_*ats 7 c# unity-container code-injection
使用默认参数注入构造函数的问题.
例如
container.RegisterType<IAddressImportRepository, AddressImportRepository>(
new PerRequestLifetimeManager());
public AddressImportRepository(
FairFlexxDbContext context, bool isUseSecurePredicate = true)
: base(context, isUseSecurePredicate)
{
}
Run Code Online (Sandbox Code Playgroud)
错误:
[InvalidOperationException:类型Boolean没有可访问的构造函数.]
Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context)+239 lambda_method(Closure,IBuilderContext)+60
Microsoft.Practices.ObjectBuilder2.<> c__DisplayClass1.b__0(IBuilderContext上下文)+71
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)+42
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)+333
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)+274
Microsoft .Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)+167
Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)+101 lambda_method(Closure,IBuilderContext)+425
Microsoft.Practices.ObjectBuilder2.<> c__DisplayClass1.b__0 (IBuilderContext context)+71
Microsoft.Practices.ObjectB uilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)+42
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)+333
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)+274
Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp (NamedTypeBuildKey newBuildKey)+167
Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)+101 lambda_method(Closure,IBuilderContext)+222
Microsoft.Practices.ObjectBuilder2.<> c__DisplayClass1.b__0(IBuilderContext context)+71
Microsoft. Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)+42
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)+333
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)+274
Microsoft.Practices.Unity.UnityContainer .DoBuildUp(Type t,Object existing,String name,IEnumerable`1 resolverOverri des)+383
Tyl*_*sen 10
Unity使用反射来确定构造函数参数类型.使用构造函数,它找到了参数FairFlexxDbContext和bool.它试图构造每种类型的实例并在bool上失败,因为它是一个值类型.它无法识别您提供的默认值以通过此错误.
为了实现这一点,您有几个选择.要么告诉你的注册表IAddressImportRepository,InjectionConstructor并告诉它如何解决你的构造函数参数或将bool参数移动为属性.由于bool是一个可选参数,我建议将其移动为属性.
container.RegisterType<IAddressImportRepository, AddressImportRepository>(
new PerRequestLifetimeManager(),
new InjectionConstructor(new ResolvedParameter<FairFlexxDbContext>(), true));
Run Code Online (Sandbox Code Playgroud)
要么
public AddressImportRepository(FairFlexxDbContext context)
: base(context, true)
{
IsUseSecurePredicate = true;
}
public bool IsUseSecurePredicate { get; set; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3725 次 |
| 最近记录: |