Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public IObjectProvider CreateScope()
        {
            var objectProvider = new ObjectProvider(this);
            var childScope     = Scope.BeginLifetimeScope(builder =>
            {
                builder.RegisterInstance <IObjectProvider>(objectProvider);
            });

            objectProvider.SetComponentContext(childScope);
            return(objectProvider);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }