Example #1
0
        public IObjectProvider Build(IServiceCollection serviceCollection = null)
        {
            if (serviceCollection != null)
            {
                Populate(serviceCollection);
            }

            _containerBuilder.Register <IObjectProvider>(context =>
            {
                var serviceProvider       = context.Resolve <IServiceProvider>() as AutofacServiceProvider;
                var componentContextField = typeof(AutofacServiceProvider).GetField("_lifetimeScope",
                                                                                    BindingFlags.NonPublic |
                                                                                    BindingFlags.Instance);
                if (componentContextField?.GetValue(serviceProvider) is IComponentContext componentContext)
                {
                    return(new ObjectProvider(componentContext));
                }
                throw new Exception("Autofac ServiceProvider not exists!");
            })
            .InstancePerLifetimeScope();
            var objectProvider = new ObjectProvider();

            objectProvider.SetComponentContext(_containerBuilder.Build());
            return(objectProvider);
        }
Example #2
0
        public IObjectProvider CreateScope()
        {
            var objectProvider = new ObjectProvider(this);
            var childScope     = Scope.BeginLifetimeScope(builder =>
            {
                builder.RegisterInstance <IObjectProvider>(objectProvider);
            });

            objectProvider.SetComponentContext(childScope);
            return(objectProvider);
        }
Example #3
0
        public IObjectProvider CreateScope(IServiceCollection serviceCollection)
        {
            var objectProvider = new ObjectProvider(this);
            var childScope     = Scope.BeginLifetimeScope(builder =>
            {
                builder.RegisterInstance <IObjectProvider>(objectProvider);
                builder.Populate(serviceCollection);
            });

            objectProvider.SetComponentContext(childScope);
            return(objectProvider);
        }
Example #4
0
        public IObjectProvider CreateScope(Action <IObjectProviderBuilder> buildAction)
        {
            if (buildAction == null)
            {
                throw new ArgumentNullException(nameof(buildAction));
            }
            var objectProvider = new ObjectProvider(this);
            var childScope     = Scope.BeginLifetimeScope(builder =>
            {
                builder.RegisterInstance <IObjectProvider>(objectProvider);
                var providerBuilder = new ObjectProviderBuilder(builder);
                buildAction(providerBuilder);
            });

            objectProvider.SetComponentContext(childScope);
            return(objectProvider);
        }
Example #5
0
 public ObjectProvider(IComponentContext componentContext, ObjectProvider parent = null)
     : this(parent)
 {
     SetComponentContext(componentContext);
 }
Example #6
0
 internal ObjectProvider(ObjectProvider parent = null)
 {
     Parent = parent;
 }