AutoFac:PropertyWiringFlags.AllowCircularDependencies做了什么?

Jur*_*uri 5 c# asp.net .net-4.0 autofac

我有一部分代码具有如下依赖关系:

public class MyPage : Page //ASPX WebForms page
{
    public IPersonBl PersonBl { get; set; }

}

public class PersonBl : IPersonBl
{

    public PersonBl(ISomeMagicBl magicBl){...}

}

public class SomeMagicBl : ISomeMagicBl
{
    public IPersonBl PersonBl { get; set; }

    public SomeMagicBl(/*Other dependencies*/) {...}
}
Run Code Online (Sandbox Code Playgroud)

我的模块配置如下所示

...
builder.RegisterAssemblyTypes(ThisAssembly).Where(t => t.Name.EndsWith("BL")).AsImplementedInterfaces().PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies).InstancePerLifetimeScope();
...
Run Code Online (Sandbox Code Playgroud)

可以看出,我在我的类中有循环依赖,我可以通过使用..PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies)...

我的问题:这个标志在幕后究竟是做什么来解决这些循环依赖?

Nic*_*rdt 3

该标志更改了从构造时到创建图的其余部分之后为类型完成属性注入的时间点。它依赖于循环中具有某种共享(单个或每个请求)的一个或多个组件 - 即使使用标志,如果所有组件都是每个依赖项实例,那么一种循环仍然存在。

如果没有该标志,Autofac 会认为组件的所有依赖项(无论是否具有属性)是让任何其他组件获取对其引用的先决条件。作为默认值,这更可靠。