StructureMap无法找到HttpServerUtility的默认实例

时间:2010-09-07 09:12:55

标签: c# structuremap ioc-container

我有一个依赖HttpServerUtilityBase我的计划的课程是让结构图使用HttpServerUtilityWrapper作为默认实例。没什么奇怪的。但是,一旦我将声明添加到我的注册表中,结构图就无法解析实例,我收到202错误。

这是我的注册表:

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,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a的配置实例
  Scoped as:Singleton

我错过了什么?

1 个答案:

答案 0 :(得分:1)

事实证明修复很简单。我需要为HttpServerUtilityWrapper

指定构造函数参数
For<HttpServerUtilityBase>()
    .Singleton()
    .Use<HttpServerUtilityWrapper>()
    .Ctor<HttpServerUtility>().Is(HttpContext.Current.Server);