c# - c# Unity 5.11.10 依赖注入 StackOverflow 异常
问题描述
我刚刚将 Unity 包升级到最新版本,当我注册 2 个 dto 对象映射器时,我得到了一个可能的循环依赖“Stackoverflow”异常。
UnityConfig.cs
public static void RegisterTypes()
{
...
Container.RegisterType<IResultMapper<Object1>, Object1DtoObject1Mapper>(new PerResolveLifetimeManager(), new InjectionMethod(nameof(IBaseMapper.Init)));
Container.RegisterType<IResultMapper<Object2>, Object2DtoObject2Mapper>(new PerResolveLifetimeManager(), new InjectionMethod(nameof(IBaseMapper.Init)));
}
Object2DtoObject2Mapper.cs
public class Object2DtoObject2Mapper: ResultMapperConfigurator<Object2Dto, Object2>
{
#region Public Methods
/// <summary>
/// Initialize the mapping configuration.
/// </summary>
public override void Init()
{
Property(dto => dto.Id, entity => entity.Id);
Collection(dto => dto.property, entity => entity.property, Object1Mapper);
Validate();
}
#endregion
#region Public Properties
#region Mappers
//error occurs here
[Dependency]
public Object1DtoObject1Mapper Object1Mapper { get; set; }
#endregion
#endregion
}
Object1DtoObject1Mapper.cs
public class Object1DtoObject1Mapper: ResultMapperConfigurator<Object1Dto, Object1>
{
#region Public Methods
/// <summary>
/// Initialize the mapping configuration.
/// </summary>
public override void Init()
{
Property(dto => dto.Id, entity => entity.Id);
Collection(dto => dto.property, entity => entity.property, Object2Mapper);
Validate();
}
#endregion
#region Public Properties
#region Mappers
//error occurs here
[Dependency]
public Object2DtoObject2Mapper Object2Mapper { get; set; }
#endregion
#endregion
}
在每个 dto 对象映射器中,都有另一个用于映射适当属性或集合的依赖项。上面的代码使用以前版本的 Unity (4.0.1) 没有产生错误。谁能告诉我如何在每个映射器中注入“Object1DtoObject1Mapper”和“Object2DtoObject2Mapper”?
解决方案
推荐阅读
- c# - 声纳云导致构建失败
- django - “NoneType”对象没有属性“id”。Django 管理员
- python - graph_tool 中的收缩顶点
- unix - 使用 awk 在文件开头添加列
- unity3d - 如何在 Unity3d 中击中对象之前稍微结束光线投射?
- reactjs - TS7016:找不到模块“my-module”的声明文件,但声明了类型
- android - lStar 上的资源链接失败
- konvajs - 在 Konva.js 上将 RGBA 过滤器应用于 SVG 图像
- python - 获取致命的 Python 错误:找不到模块:没有名为编码的模块
- c++ - 使用字符串数组和整数数组按升序排列数组