Unity无法解析InterfaceInterceptor的依赖关系

时间:2014-07-17 15:16:11

标签: c# unity-container

我编写了一个Unity接口IInterceptionBehavior的实现来做一些日志记录。它依赖于ILog来进行日志记录。

public class InterceptionLoggingBehavior : IInterceptionBehavior {
  public InterceptionLoggingBehavior(ILog log) {...}
  ...
}

我还有一个实现ConsoleLog的{​​{1}}。

我正在尝试解析使用日志记录接口拦截器的接口,但找不到ILog。尽管我可以团结一致直接解决ILog,但尝试直接解决InterceptionLoggingBehavior无法正常工作:

ILog

解析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>(); (在第二行)工作正常,但解析了第3行的ILogInterceptionLoggingBehavior(我尝试记录的界面)第4行得到错误:

  

当前类型com.InsightSoftware.LoggingAPI.ILog是一个接口   并且无法构建。你错过了类型映射吗?

我的问题:有谁可以告诉我为什么当ITableIdentityProvider依赖{}时,团结无法解析ILog

我用来配置统一的xml,包括映射:

InterceptionLoggingBehavior

(请注意,我从未明确注册<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> ,我认为通过在InterceptionLoggingBehavior标记中使用它来隐式注册。)

我也尝试在代码中配置单位(不使用配置文件),如下所示:

interceptionBehavior

但我仍然得到同样的错误。

修改/更新 我已经做了一些挖掘并尝试更换我用

解析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>(); 的行
InterceptionLoggingBehavior

我收到了错误

  

依赖项的解析失败,输入= \“com.InsightSoftware.Logging.InterceptionLoggingBehavior \”,name = \“(none)\”。

     

发生异常时:解析构造函数com.InsightSoftware.Logging.InterceptionLoggingBehavior(com.InsightSoftware.LoggingAPI.ILog log)的参数\“log \”。

     

异常是:InvalidCastException - 无法将类型为'HubbleSandbox.ConsoleLog'的对象强制转换为'com.InsightSoftware.LoggingAPI.ILog'。

我已经检查过类型是否可分配。

container.Resolve<InterceptionLoggingBehavior>(
  new ParameterOverride(
    "log", 
    container.Resolve<ILog>()));

public class ConsoleLog : ILog {...

都不会导致任何错误。我还检查了名称空间是否正确(ILog l = new ConsoleLog(); 是一个常见的接口名称 - 我们使用ILog作为ILogILog的外观 - 所以我有三重检查)。

使用

  • .Net Framework 4.5
  • Unity 2.1.505.0(我们遇到最新的Unity on mono问题)
  • 某些依赖项来自内部NuGets - 不确定这是否是一个因素

1 个答案:

答案 0 :(得分:1)

经过多一点挖掘后,我找到了自己问题的答案,所以如果其他人发现他们遇到了完全相同的问题,我会张贴它。

我使用的设置涉及三个独立的解决方案:

  1. 日志API解决方案(非常简单)
  2. 使用统一接口拦截进行日志记录的API的实现(依赖于1)
  3. 将两者连接在一起的沙箱,提供ILog的默认实现(依赖于1和2)
  4. 1和2是通过NuGets

    提供的

    我遇到的问题是我更新了API项目的程序集名称(在项目属性中)(从LoggingAPIcom.InsightSoftware.LoggingAPI)。然后我推送了API NuGet,然后在沙盒项目中获得了最新的NuGet - 但是我不记得在实现项目中获得最新的NuGet并重新推送它。

    因此,实现正在寻找LoggingAPI程序集中的类,但从com.InsightSoftware.LoggingAPI程序集中获取类,因此类型不匹配且unity无法解析正确的类型

相关问题