0

I have a class that relies on HttpServerUtilityBase my plan was to get structure map to use HttpServerUtilityWrapper as the default instance. Nothing odd there. However, once I've added the declarations to my registry, structuremap is unable to resolve the instance and I get a 202 error.

This is my registry:

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>();
    }
}

It all looks fine to me, but clearly I'm missing something. Also the line from the generated by calling WhatDoIHave() that refers to HttpServerUtilityBase seems to have a reference to HttpServerUtilityWrapper so I guess it should just work.


HttpServerUtilityBase (System.Web.HttpServerUtilityBase) 3bf840df-e159-4dcf-93ef-211bb7484698 Configured Instance of System.Web.HttpServerUtilityWrapper, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Scoped as: Singleton

What am I missing?

1
  • 2
    FYI - you do not need to scan the assembly containing HttpServerUtilityBase. Scanning is only used to register types by convention. In this case, you are explicitly registering the HttpServerUtiltityBase type, so the convention scanning doesn't buy you anything. Commented Sep 7, 2010 at 16:47

1 Answer 1

1

It turns out the fix is simple. I need to specify the constructor argument for the HttpServerUtilityWrapper

For<HttpServerUtilityBase>()
    .Singleton()
    .Use<HttpServerUtilityWrapper>()
    .Ctor<HttpServerUtility>().Is(HttpContext.Current.Server);
Sign up to request clarification or add additional context in comments.

1 Comment

For the ninjecteers: kernel.Bind<HttpServerUtility>().ToConstant(HttpContext.Current.Server); kernel.Bind<HttpServerUtilityBase>().To<HttpServerUtilityWrapper>().InSingletonScope();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.