AutofacChildLifetimeScopeServiceProviderFactory

We found 10 examples in language CSharp for this search. You will see 26 fragments of code.
        [Fact]
        public void CreateBuilderReturnsNewInstance()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var configurationAdapter = factory.CreateBuilder(new ServiceCollection());

            Assert.NotNull(configurationAdapter);
        }

        [Fact]
        public void CreateBuilderAllowsForNullConfigurationAction()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var configurationAdapter = factory.CreateBuilder(new ServiceCollection());

            Assert.NotNull(configurationAdapter);
        }

        [Fact]
        public void CreateServiceProviderThrowsWhenProvidedNullAdapter()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var exception = Assert.Throws<ArgumentNullException>(() => factory.CreateServiceProvider(null));

            Assert.Equal("configurationAdapter", exception.ParamName);
        }

        [Fact]
        public void CreateServiceProviderReturnsAutofacServiceProvider()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var serviceProvider = factory.CreateServiceProvider(new AutofacChildLifetimeScopeConfigurationAdapter());

            Assert.IsType<AutofacServiceProvider>(serviceProvider);
        }

        [Fact]
        public void CreateBuilderReturnsNewInstance()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var configurationAdapter = factory.CreateBuilder(new ServiceCollection());

            Assert.NotNull(configurationAdapter);
        }

        [Fact]
        public void CreateBuilderAllowsForNullConfigurationAction()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var configurationAdapter = factory.CreateBuilder(new ServiceCollection());

            Assert.NotNull(configurationAdapter);
        }

        [Fact]
        public void CreateServiceProviderThrowsWhenProvidedNullAdapter()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var exception = Assert.Throws<ArgumentNullException>(() => factory.CreateServiceProvider(null));

            Assert.Equal("containerBuilder", exception.ParamName);
        }

        [Fact]
        public void CreateServiceProviderReturnsAutofacServiceProvider()
        {
            var factory = new AutofacChildLifetimeScopeServiceProviderFactory(GetRootLifetimeScope);

            var serviceProvider = factory.CreateServiceProvider(new AutofacChildLifetimeScopeConfigurationAdapter());

            Assert.IsType<AutofacServiceProvider>(serviceProvider);
        }


        public static async Task Main(string[] args)
        {
            var b = new ContainerBuilder();
            b.RegisterAllAssemblyModules();
            using var c = b.Build();

            await Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacChildLifetimeScopeServiceProviderFactory(c))
                .ConfigureWebHostDefaults(w => w.UseStartup<Startup>())
                .RunConsoleAsync();
        }


        /// <summary>
        /// Main  application entry point.
        /// </summary>
        /// <param name="args"></param>
        public static async Task Main(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterAllAssemblyModules();

            using (var container = builder.Build())
                await Host.CreateDefaultBuilder(args)
                    .UseServiceProviderFactory(ctx => new AutofacChildLifetimeScopeServiceProviderFactory(container, b => b.RegisterAllAssemblyModules()))
                    .ConfigureWebHostDefaults(b => b
                        .UseStartup<Startup>())
                    .RunConsoleAsync();
        }

        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {

            var container = AutofacBootstraper.Execute();
            return new HostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .ConfigureHostConfiguration(ConfigureHost(args))
                .UseServiceProviderFactory(new AutofacChildLifetimeScopeServiceProviderFactory(container))
                .ConfigureWebHostDefaults(ConfigureWebHost());
           
        }
        public static Action<IWebHostBuilder> ConfigureWebHost()
        {
            return webBuilder =>
            {
                webBuilder.UseStartup<Startup>();

            };
        }
        public static Action<IConfigurationBuilder> ConfigureHost(string[] args)
        {
            return config =>
            {
                if (args != null)
                    config.AddCommandLine(args);
            };
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();

            app.UseEndpoints(builder => builder.MapControllers());
        }

        /// <summary>
        /// This method is called by the <see cref="IHost"/> since we are registering <see cref="AutofacChildLifetimeScopeServiceProviderFactory"/> for Host B.
        /// </summary>
        /// <param name="configurationAdapter">An adapter that can take several registrations for the scope used in Application B.</param>
        public void ConfigureContainer(AutofacChildLifetimeScopeConfigurationAdapter configurationAdapter)
        {
            // You must use an AutofacChildLifetimeScopeConfigurationAdapter
            // here instead of a ContainerBuilder.
            configurationAdapter.Add(
                builder => builder
                    .RegisterType<ApplicationBScopedDependency>()
                    .AsSelf()
                    .InstancePerLifetimeScope()
            );
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();

            app.UseEndpoints(builder => builder.MapControllers());
        }

        /// <summary>
        /// This method is called by the <see cref="IHost"/> since we are registering <see cref="AutofacChildLifetimeScopeServiceProviderFactory"/> for Host A.
        /// </summary>
        /// <param name="configurationAdapter">An adapter that can take several registrations for the scope used in Application A.</param>
        public void ConfigureContainer(AutofacChildLifetimeScopeConfigurationAdapter configurationAdapter)
        {
            // You must use an AutofacChildLifetimeScopeConfigurationAdapter
            // here instead of a ContainerBuilder.
            configurationAdapter.Add(
                builder => builder
                    .RegisterType<ApplicationAScopedDependency>()
                    .AsSelf()
                    .InstancePerLifetimeScope()
                );
        }


        protected override void ConfigureClient(HttpClient client)
        {
            base.ConfigureClient(client);
        }
        protected override IHostBuilder CreateHostBuilder()
        {
            container = AutofacBootstraper.Execute();

            var host = base.CreateHostBuilder()
            .ConfigureWebHostDefaults(webHostBuilder =>
            {
                webHostBuilder
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseStartup<Startup>();
            })
         
            .UseServiceProviderFactory(new AutofacChildLifetimeScopeServiceProviderFactory(container));
            return host;
        }

        public void Start(IContainer container)
        {
            var host = Host.CreateDefaultBuilder(Environment.GetCommandLineArgs())
                .UseServiceProviderFactory(
                    new AutofacChildLifetimeScopeServiceProviderFactory(
                        container.BeginLifetimeScope("root-one")))
                .ConfigureWebHostDefaults(webHostBuilder => { webHostBuilder.UseStartup<CoriumAspCoreStartup>(); })
                .Build();
            host.RunAsync();
        }


            public void ConfigureServices(IServiceCollection services)
            {
                
                services.AddRazorPages()
                    .WithRazorPagesRoot("/Pages")
                    .AddJsonOptions(options =>
                        options.JsonSerializerOptions.WriteIndented = _environment.IsDevelopment());
            }


            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                    app.UseDeveloperExceptionPage();
                else
                {
                    app.UseExceptionHandler("/Error");
                    app.UseHsts();
                }

                app.UseStatusCodePages();
                app.UseHttpsRedirection();
                app.UseRouting();
                app.UseAuthorization();
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapRazorPages();
                    endpoints.MapControllers();
                });

                foreach (string allowedStaticDirectory in _allowedStaticDirectories)
                {
                    app.UseStaticFiles(new StaticFileOptions
                    {
                        RequestPath = $"/{allowedStaticDirectory}",
                        FileProvider =
                            new PhysicalFileProvider(
                                Path.Combine(Environment.CurrentDirectory,
                                    allowedStaticDirectory)),
                        ServeUnknownFileTypes = true
                    });
                }
            }


        /// <summary>
        /// Adds a configuration action that will be executed when the child <see cref="ILifetimeScope"/> is created.
        /// </summary>
        /// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/> that adds component registrations to the container.</param>
        /// <exception cref="ArgumentNullException">Throws when the passed configuration-action is null.</exception>
        public void Add(Action<ContainerBuilder> configurationAction)
        {
            if (configurationAction == null) throw new ArgumentNullException(nameof(configurationAction));

            _configurationActions.Add(configurationAction);
        }

Autofac.Extensions.DependencyInjection.AutofacChildLifetimeScopeServiceProviderFactory : IServiceProviderFactory

Constructors :

public AutofacChildLifetimeScopeServiceProviderFactory(Func<ILifetimeScope> rootLifetimeScopeAccessor = , Action<ContainerBuilder> configurationAction = null)
public AutofacChildLifetimeScopeServiceProviderFactory(ILifetimeScope rootLifetimeScope = , Action<ContainerBuilder> configurationAction = null)

Methods :

public AutofacChildLifetimeScopeConfigurationAdapter CreateBuilder(IServiceCollection services = )
public IServiceProvider CreateServiceProvider(AutofacChildLifetimeScopeConfigurationAdapter containerBuilder = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()

Other methods