Register Container Itself Using Autofac

前端 未结 3 1245
北恋
北恋 2021-01-01 18:54

I was wondering is there\'s any side effect to registering the container within itself

IContainer container;
ContainerBuilder builder = new ContainerBuilder         


        
3条回答
  •  醉话见心
    2021-01-01 19:39

    You can use this extension method:

    public static void RegisterSelf(this ContainerBuilder builder)
    {
        IContainer container = null;
        builder.Register(c => container).AsSelf().SingleInstance();
        builder.RegisterBuildCallback(c => container = c);
    }
    

    use it like this: builder.RegisterSelf();

提交回复
热议问题