在子生命周期中继承 autofac 模块

发布于 2024-11-26 16:14:21 字数 1923 浏览 0 评论 0原文

我有一个模块可以为某些特定类型(例如 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);
    }
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-12-03 16:14:21

这是一个已知的错误:
http://code.google.com/p/autofac/issues/detail ?id=218

您可以在讨论中找到一些解决方法。

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

You can find some workarounds there in discussion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文