I am trying to inject dependencies into aspx codebehind using BuildUp, but it intermittently throws an error saying "X is an interface and cannot be constructed. Are you missing a type mapping"
I build my container in the global.asax. Then I created a basepage that all pages inherit from. Overriding the OnPreInit method, I do the following:
var context = HttpContext.Current;
if (context == null)
{
return;
}
var accessor = context.ApplicationInstance as IContainerAccessor;
if (accessor == null)
{
return;
}
var container = accessor.Container;
if (container == null)
{
throw new InvalidOperationException("No container found.");
}
container.BuildUp(this as T);
"this" is the page that is inheriting from BasePage. I then use the Dependency attribute to decorate all dependencies in the actual aspx.cs that I want injected. If I do a clean and run it, it works fine. But if I do a refresh or the screen is triggered to refresh, it blows up with the error above.
Not sure what it's problem is. Maybe it doesn't like BuildUp being called twice.
More detail.
The type "X" is one of my custom interfaces. This is just a sample project I am doing to test something, but it looks like so:
public class BusObjectA : IBusObjectA
So it's pretty straightfoward.
In my sample aspx page, I have the following that I am trying to resolve via the BuildUp call:
[Dependency]
public IBusObjectA BusinessObjectA { get; set; }
Container creation:
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterTypes(AllClasses.FromLoadedAssemblies().Where(x => x.Namespace != null && x.Namespace.Contains("InjectionTest")),
WithMappings.FromMatchingInterface,
WithName.Default,
WithLifetime.ContainerControlled,
t => new InjectionMember[]
{
new Interceptor<TransparentProxyInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>()
});