开发者

inherit autofac module in child lifetime scope

I have a module which does property injection for some particular type such as ILog.

protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) 
{
        registration.Activated += [doing property injection];
}

It works fine within the same scope, but in a child scope, AttachToComponentRegistration won't be triggered anymore, I have to register the module again in order to enable property injection.

so my question is how to inherit registered module in child lifetime scope? or is there any other way to do this?

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"开发者_如何学C);
    }
}

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);
    }
}


This is a known bug: http://code.google.com/p/autofac/issues/detail?id=218

You can find some workarounds there in discussion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜