【问题标题】:Unity unable to resolve a dependency of InterfaceInterceptorUnity 无法解决 InterfaceInterceptor 的依赖关系
【发布时间】:2014-07-17 15:16:10
【问题描述】:

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

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

我还有一个实现ILogConsoleLog

我正在尝试解析使用日志接口拦截器的接口,但它找不到ILog。即使我可以统一直接解决ILog,尝试直接解决InterceptionLoggingBehavior也不起作用:

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

解析ILog(第二行)工作正常,但解析第三行的InterceptionLoggingBehavior 或第四行的ITableIdentityProvider(我尝试登录的界面)会出现错误:

当前类型 com.InsightSoftware.LoggingAPI.ILog 是一个接口 并且无法构建。您是否缺少类型映射?

我的问题:谁能告诉我为什么当 ILog 依赖于 InterceptionLoggingBehavior 时,Unity 无法解决它?

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

<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 的行
container.Resolve<InterceptionLoggingBehavior>(
  new ParameterOverride(
    "log", 
    container.Resolve<ILog>()));

我得到了错误

依赖关系解析失败,type = \"com.InsightSoftware.Logging.InterceptionLoggingBehavior\",name = \"(none)\"。

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

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

我已经检查过这些类型是可分配的。

public class ConsoleLog : ILog {...

ILog l = new ConsoleLog();

不会导致任何错误。我还检查了命名空间是否正确(ILog 是一个通用接口名称——我们使用我们的ILog 作为log4NetILog 的外观——所以我进行了三次检查)。

使用

  • .Net Framework 4.5
  • Unity 2.1.505.0(我们在使用最新的 Unity 单声道时遇到问题)
  • 一些依赖来自内部 NuGet - 不确定这是否是一个因素

【问题讨论】:

  • 有趣。我的第一个想法是拦截器是基础设施的一个元素,而不是可解析的服务之一。而且我不确定 Unity 是否将相同的解析逻辑应用于您的业务类和基础设施类。换句话说,如果您以显式方式解析类型,它就可以工作。如果 Unity 在内部使用它作为拦截器,你确定它使用同一个容器来解决它吗?
  • 好主意,但是当我从注册 ILog 的完全相同的容器中解析 InterceptionLoggingBehavior 时出现错误。
  • 好的,我已经创建了一个符合您的场景的最小设置,它适用于我。没有例外。我认为您正在做一些更具体的事情。

标签: c# unity-container


【解决方案1】:

经过一番挖掘,我找到了自己问题的答案,所以我会发布它,以防其他人发现他们有完全相同的问题。

我使用的设置涉及三个不同的解决方案:

  1. 日志 API 解决方案(非常简单)
  2. 使用统一接口拦截进行日志记录的 API 实现(依赖于 1)
  3. 将两者联系在一起并提供ILog 的默认实现的沙盒(依赖于 1 和 2)

1 和 2 是通过 NuGet 提供的

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

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

【讨论】:

    猜你喜欢
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2017-02-20
    相关资源
    最近更新 更多