我有两项需要XPathDocument
的服务。我希望能够定义XPathDocumnet
的命名实例,以便在两个服务的配置中使用。我还希望能够告诉StuctureMap使用XPathDocument
的构造函数。当我尝试获取XPathDocument
的实例时,它告诉我它找不到XmlReader
的插件类型。我想使用需要字符串uri的构造函数来获取xml文件。我似乎无法让这个工作。这是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");
});
}
答案 0 :(得分:2)
看看this。简而言之,您需要将OfConcreteType<Service1>()
更改为ConstructedBy(() => new Service1());
。