【问题标题】:How to resolve dependencies in referenced library class如何解决引用库类中的依赖关系
【发布时间】:2021-10-21 14:11:24
【问题描述】:

ASP.Net Webforms 应用程序 (.Net Framework 4.7.2) 引用了用 .Net 标准 2.0 编写的 dll。在 dll 中,我有一个接口 ISomething,它具有对 IAnother 的属性依赖。

使用 Autofac。在Global.asax Application_Start 我正在注册实现ISomethingIAnother 的类。

在执行时,我确实得到了 ISomtething 的正确解析;但它的依赖 IAnother 最终是 null (我猜是未解决)。

我没有在ISometing 中添加任何 Autofac 代码行;刚刚将IAnother 添加为公共属性;我希望两者都能被Autofac.Web 代码自动解决。 (这是正确的吗?)

编辑:

我更改了实现 ISomething 的类的实现,以接收 IAnother 作为构造函数中的参数(而不是作为属性)......并且有效。

ISomething 是 WebForm 页面上的属性依赖项。 IAnother 作为对 ISomething 实现的属性依赖

ISomething 是对 WebForm 页面的属性依赖项。 IAnother 作为构造函数依赖于 ISomething 的实现

我想了解为什么它以一种方式而不是另一种方式工作。

【问题讨论】:

    标签: asp.net webforms autofac .net-standard-2.0


    【解决方案1】:

    Autofac 中的属性依赖是可选的,因此您必须在注册时选择加入。

    所以在你的情况下:

    builder.RegisterType<Something>()
        .As<ISomething>()
        .PropertiesAutowired();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      • 2023-03-11
      • 2022-11-09
      • 2018-12-08
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多