我有一个模块可以为某些特定类型执行属性注入,例如ILog。
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
{
registration.Activated += [doing property injection];
}
它在同一范围内工作正常,但在子范围内,AttachToComponentRegistration将不再被触发,我必须再次注册该模块才能启用属性注入。
所以我的问题是如何在子生命周期范围内继承已注册的模块?或者还有其他方法吗?
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterModule(new TestModule());
builder.RegisterType<Test>().As<ITest>();
var container = builder.Build();
container.Resolve<ITest>().Say(); // output test11111
var scope = container.BeginLifetimeScope("nested", b =>
{
// b.RegisterModule(new TestModule());
b.RegisterType<Test2>().As<ITest2>();
});
scope.Resolve<ITest>().Say();
scope.Resolve<ITest2>().Say();
}
}
public interface ITest
{
void Say();
}
public class Test : ITest
{
public void Say()
{
Console.WriteLine("test1111111");
}
}
public interface ITest2
{
void Say();
}
public class Test2 : ITest2
{
public void Say()
{
Console.WriteLine("test2222222");
}
}
public class TestModule : Module
{
protected override void AttachToComponentRegistration(Autofac.Core.IComponentRegistry componentRegistry, Autofac.Core.IComponentRegistration registration)
{
Console.WriteLine("called for " + registration.Activator.LimitType);
base.AttachToComponentRegistration(componentRegistry, registration);
}
}
答案 0 :(得分:2)
这是一个已知的错误: http://code.google.com/p/autofac/issues/detail?id=218
您可以在讨论中找到一些解决方法。