2
我有一個模塊,它爲某些特定類型(如ILog)進行屬性注入。繼承兒童生命週期範圍內的autofac模塊
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);
}
}
你在做什麼Autofac版本? –
存在一個可能相關的問題:http://code.google.com/p/autofac/issues/detail?id=218 ...另外,您可以發佈在子範圍中創建/註冊組件的代碼?謝謝! –
Nicholas,請查看上面的源代碼,「調用」不出現嵌套的作用域組件。 –