StructureMap can not find a default instance for HttpServerUtility
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, PublicKeyTok开发者_如何学Cen=b03f5f7f11d50a3a
Scoped as: Singleton
What am I missing?
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);
精彩评论