I have written an implementation of the unity interface IInterceptionBehavior to do some logging. It has a dependency on an ILog to do the logging.
public class InterceptionLoggingBehavior : IInterceptionBehavior {
public InterceptionLoggingBehavior(ILog log) {...}
...
}
I also have a ConsoleLog that implements ILog.
I am trying to resolve an interface that uses the logging interface interceptor, but it cannot find the ILog. Trying to resolve the InterceptionLoggingBehavior directly does not work even though I can get unity to resolve the ILog directly:
UnityContainer container = ...
var l = container.Resolve<com.InsightSoftware.LoggingAPI.ILog>();
var b = container.Resolve<com.InsightSoftware.Logging.InterceptionLoggingBehavior>();
var p = container.Resolve<com.InsightSoftware.MetadataAPI.ITableIdentityProvider>();
Resolving the ILog (on the second line) works fine, but resolving the InterceptionLoggingBehavior on the 3rd line or the ITableIdentityProvider (the interface that I am trying to log) on the 4th line gets the error:
The current type, com.InsightSoftware.LoggingAPI.ILog, is an interface and cannot be constructed. Are you missing a type mapping?
My Question: Can anyone tell me why unity cannot resolve the ILog when it is a dependency for InterceptionLoggingBehavior?
The xml that I am using to configure unity, including the mapping:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
<alias alias="ILog" type="com.InsightSoftware.LoggingAPI.ILog, com.InsightSoftware.LoggingAPI"/>
<alias alias="ConcreteLog" type="HubbleSandbox.ConsoleLog, HubbleSandbox" />
<alias alias="InterceptionLoggingBehavior" type="com.InsightSoftware.Logging.InterceptionLoggingBehavior, com.InsightSoftware.Logging" />
<!--More aliases-->
<containers>
<container>
<extension type="Interception" />
<!-- The type mapping that I expect to be resolved. -->
<register type="ILog" mapTo="ConcreteLog" />
<register type="ITableIdentityProvider" mapTo="TableIdentityProvider">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="InterceptionLoggingBehavior" />
</register>
<!--More registrations-->
</container>
</containers>
(note that I never explicitly register InterceptionLoggingBehavior, I think that it gets implicitly registered by using it in the interceptionBehavior tag.)
I've also tried configuring unity in code (not using a config file) like so:
UnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<ILog, ConsoleLog>();
container.RegisterType<ITableIdentityProvider, TableIdentityProvider>(
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<InterceptionLoggingBehavior>());
// more registrations
var l = container.Resolve<ILog>();
var b = container.Resolve<ITableIdentityProvider>();
but I still get the same error.
Edit/Update
I've done some more digging and tried replacing the line where I resolve the InterceptionLoggingBehavior with
container.Resolve<InterceptionLoggingBehavior>(
new ParameterOverride(
"log",
container.Resolve<ILog>()));
and I get the error
Resolution of the dependency failed, type = \"com.InsightSoftware.Logging.InterceptionLoggingBehavior\", name = \"(none)\".
Exception occurred while: Resolving parameter \"log\" of constructor com.InsightSoftware.Logging.InterceptionLoggingBehavior(com.InsightSoftware.LoggingAPI.ILog log).
Exception is: InvalidCastException - Unable to cast object of type 'HubbleSandbox.ConsoleLog' to type 'com.InsightSoftware.LoggingAPI.ILog'.
I've checked that the types are assignable.
public class ConsoleLog : ILog {...
and
ILog l = new ConsoleLog();
Neither cause any error. I've also checked that the namespaces are correct (ILog is a common interface name - we are using our ILog as a facade to the ILog in log4Net - so I have triple checked).
Using
- .Net Framework 4.5
- Unity 2.1.505.0 (we are having problems with the latest Unity on mono)
- Some dependencies are from internal NuGets - not sure if this is a factor
InterceptionLoggingBehaviorfrom the exact same container as I register theILog.