【问题标题】:Tell StructureMap to use a specific constructor告诉 StructureMap 使用特定的构造函数
【发布时间】:2010-09-26 11:02:55
【问题描述】:

我有两项服务需要XPathDocument。我希望能够定义 XPathDocumnet 的命名实例以在两个服务的配置中使用。我还希望能够告诉 StuctureMap 使用 XPathDocument 的哪个构造函数。当我尝试获取XPathDocument 的实例时,它告诉我它找不到XmlReader 的插入类型。我想使用需要 xml 文件的字符串 uri 的构造函数。我似乎无法让它发挥作用。这是 StructureMap 配置代码。

public class Service1 : IService1 {
    public Service1(XPathDocument document) {}
}
public class Service2 : IService2 {
    public Service2(XPathDocument document) {}
}

public class Registry1 : Registry {
    ForRequestedType<IService1>().TheDefault.Is.OfConcreteType<Service1>()
        .CtorDependency<XPathDocument>()
        .Is(x => x.TheInstanceNamed("XPathDocument1"));
    ForRequestedType<IService2>().TheDefault.Is.OfConcreteType<Service2>()
        .CtorDependency<XPathDocument>()
        .Is(x => x.TheInstanceNamed("XPathDocument2"));

    ForRequestedType<XPathDocument>().AddInstances(x => {
        x.OfConcreteType<XPathDocument>()
            .WithCtorArg("uri").EqualToAppSetting("XmlFile1")
            .WithName("XPathDocument1");
        x.OfConcreteType<XPathDocument>()
            .WithCtorArg("uri").EqualToAppSetting("XmlFile2")
            .WithName("XPathDocument2");
    });
}

【问题讨论】:

标签: c# structuremap xmlreader xpathdocument


【解决方案1】:

看看this。总之,你需要将OfConcreteType&lt;Service1&gt;()改为ConstructedBy(() =&gt; new Service1());

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多