代码之家  ›  专栏  ›  技术社区  ›  ilivewithian

StructureMap找不到HttpServerUtility的默认实例

  •  0
  • ilivewithian  · 技术社区  · 14 年前

    我有一门课 HttpServerUtilityBase HttpServerUtilityWrapper 作为默认实例。没什么奇怪的。但是,一旦我将声明添加到注册表中,structuremap就无法解析该实例,并且出现了一个错误。

    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程序


    HttpServer实用程序库(System.Web.HttpServer实用程序库)3bf840df-e159-4dcf-93ef-211bb7484698配置的实例System.Web.HttpServerUtilityWrapper文件, 系统.Web,版本=4.0.0.0,区域性=中性,PublicKeyToken=b03f5f7f11d50a3a
    范围为:单例

    1 回复  |  直到 14 年前
        1
  •  1
  •   ilivewithian    14 年前

    结果发现解决办法很简单。我需要为 HttpServerUtilityWrapper

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