2010-09-07 66 views
0

我有一個依賴於HttpServerUtilityBase的類,我的計劃是讓結構圖使用HttpServerUtilityWrapper作爲默認實例。沒有什麼奇怪的。但是,一旦我將聲明添加到註冊表中,structuremap無法解析該實例,並且出現202錯誤。StructureMap找不到HttpServerUtility的默認實例

這是我的註冊表:

public class ApplicationRegistry : Registry 
{ 
    public ApplicationRegistry() 
    { 
     Scan(scanThe => 
     { 
      scanThe.AssembliesFromApplicationBaseDirectory(); 
      scanThe.WithDefaultConventions(); 
     }); 

     Scan(scanThe => 
     { 
      scanThe.AssemblyContainingType<HttpServerUtilityBase>(); 
     }); 

     SetAllProperties(x => 
     { 
      x.WithAnyTypeFromNamespaceContainingType<IFinancialYearRepository>(); 
      x.WithAnyTypeFromNamespaceContainingType<IUserManagementFacade>(); 
      x.WithAnyTypeFromNamespaceContainingType<IBulkTypeImporter>(); 
      x.OfType<ILog>(); 
      x.OfType<ISessionManager>(); 
     }); 

     For<IUnitOfWork>() 
      .HttpContextScoped() 
      .Use(() => new EFUnitOfWork(
        ConfigurationManager.ConnectionStrings["PublishedEFSqlServer"].ConnectionString 
        )).Named("publishedUnitOfWork"); 

     For<IUnitOfWork>() 
      .HttpContextScoped() 
      .Use(() => new EFUnitOfWork(
       ConfigurationManager.ConnectionStrings["UnpublishedEFSqlServer"].ConnectionString 
       )).Named("unpublishedUnitOfWork"); 

     For<ILog>() 
      .AlwaysUnique() 
      .Use(s => 
      { 
       ILog loggger; 
       if (s.ParentType == null) 
       { 
        loggger = LogManager.GetLogger(s.BuildStack.Current.ConcreteType); 
       } 
       else 
       { 
        loggger = LogManager.GetLogger(s.ParentType); 
       } 

       if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated) 
       { 
        ThreadContext.Properties["user"] = HttpContext.Current.User.Identity.Name; 
       } 
       return loggger; 
      }); 

     For<ISessionManager>().Singleton(); 

     For<HttpServerUtilityBase>() 
      .Singleton() 
      .Use<HttpServerUtilityWrapper>(); 
    } 
} 

這一切看起來好像沒什麼問題,但顯然我失去了一些東西。另外,通過調用WhatDoIHave()生成的引用HttpServerUtilityBase的行似乎對HttpServerUtilityWrapper有參考,所以我猜它應該工作。


的HttpServerUtilityBase(System.Web.HttpServerUtilityBase)3bf840df-e159-4dcf-93ef-211bb7484698 System.Web.HttpServerUtilityWrapper,System.Web程序的配置的情況下,版本= 4.0.0.0,文化=中性公鑰= b03f5f7f11d50a3a
作用域爲:辛格爾頓

我缺少什麼?

+2

僅供參考 - 你不需要掃描包含的HttpServerUtilityBase大會。掃描僅用於按照慣例註冊類型。在這種情況下,您正在顯式註冊HttpServerUtiltityBase類型,所以約定掃描不會爲您購買任何東西。 – 2010-09-07 16:47:34

回答

1

事實證明,修復很簡單。我需要指定構造函數的參數爲​​HttpServerUtilityWrapper

For<HttpServerUtilityBase>() 
    .Singleton() 
    .Use<HttpServerUtilityWrapper>() 
    .Ctor<HttpServerUtility>().Is(HttpContext.Current.Server); 
+0

對於ninjecteers:。 'kernel.Bind ()ToConstant(HttpContext.Current.Server);' 'kernel.Bind <的HttpServerUtilityBase>()爲了()InSingletonScope(); ' – 2011-07-11 04:50:18

相關問題