我使用 Autofac nuget 包 Autofac.Web
继承了一些旧的 ASP.NET Webforms 代码。我注意到该配置定义了 PropertyInjection
模块和 AttributedInjection
模块 - 违反了文档 here其中明确指出:
First, remove the
PropertyInjectionModule
from your web.config file and replace it with theAttributedInjectionModule
但是 web.config 中的 autofac 条目如下所示(在 httpModules
中具有类似的设置):
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="ContainerDisposal" ... etc .../>
<!-- Should next line be removed? -->
<add name="PropertyInjection" ... etc .../>
<add name="AttributedInjection" ... etc .../>
</modules>
</system.webServer>
应用程序中的页面通常使用[InjectUnsetProperties]
属性。
在大多数情况下,应用程序似乎可以正常工作 - 但有一些奇怪的问题可能与 DI 相关。更改配置以遵循文档显然会涉及大量的重新测试工作 - 不用说,在旧代码中自动化测试并不广泛。
我没有在 Webforms 中使用 Autofac 的经验 - 谁能解释一下上述错误配置的影响以及它可能导致什么问题(如果有)?另外,相反,删除 PropertyInjection
模块的潜在影响是什么?
最佳答案
如果两个模块都已就位,则两个模块都将运行并且都将执行注入(inject)工作。
问题在于 AttributedInjectionModule
更具限制性 - 它只在您明确告诉它的地方注入(inject) - 而 PropertyInjectionModule
具有更广泛的覆盖范围 - 它总是注入(inject)。两者同时存在将导致属性失去控制,因为 PropertyInjectionModule
无论如何都会注入(inject)所有内容。
您还可能会遇到某些依赖项类型被实例化两次的问题 - 为管道中注入(inject)的每个模块实例化一次。
如果您删除PropertyInjectionModule
,那么只有属性的东西才会被注入(inject),并且您将保留更精细的控制。
关于c# - Autofac.Web : Impact of having *both* PropertyInjection and AttributedInjection configured,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335946/