StructureMap类链接 - 堆栈溢出或其他错误

时间:2009-09-17 18:56:08

标签: stack-overflow structuremap

这让我对许多配置感到困惑。我一直在阅读文档,我只是不明白。这是我的注册码:

ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();

ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
    .WithCtorArg("busyDelay").EqualTo(TimeSpan.FromMilliseconds(20))
    .WithCtorArg("overallTimeout").EqualTo(TimeSpan.FromSeconds(60));

ForRequestedType<WorkItemQueue>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());

实际上,它说没有IWorkItemProcessor的默认实例(这是正确的)。将最后一行切换为:

ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());

...造成堆栈溢出异常。

如何将实现接口的类链接在一起,并在构造函数中接受相同的接口?

1 个答案:

答案 0 :(得分:0)

这有效,但我无法解释原因。据我所知,第一个版本应该也可以正常工作。

ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();

var retryProcessor = ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
    .CtorDependency<TimeSpan>("busyDelay").Is(x => x.Object(TimeSpan.FromMilliseconds(20)))
    .CtorDependency<TimeSpan>("overallTimeout").Is(x => x.Object(TimeSpan.FromSeconds(60)));

ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
    .CtorDependency<IWorkItemProcessor>("workItemProcessor").Is(x => x.Instance(retryProcessor));