2010-09-07 67 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

相关问题