У меня есть модуль, который выполняет внедрение свойства для определенного типа, такого как 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);
}
}