2008-12-11 21 views
38

我有兩個服務需要XPathDocument。我希望能夠定義XPathDocumnet的命名實例以用於配置這兩個服務。我也想要告訴StuffMap使用XPathDocument的構造函數。當我嘗試獲取XPathDocument的實例時,它告訴我它無法找到XmlReader的插入類型。我想使用需要一個字符串uri作爲xml文件的構造函數。我似乎無法得到這個工作。這是StructureMap配置代碼。Tell 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

[StructureMap:如何通過代碼定義默認構造函數?]的可能的副本(http://stackoverflow.com/questions/289512/structuremap-how-to-define-default-constructor-by-code) – 2012-04-03 15:33:41

回答

2

看看this。總之,您需要將OfConcreteType<Service1>()更改爲ConstructedBy(() => new Service1());

相關問題