From 380b08798bf724f36e200e687129b44a0778ab92 Mon Sep 17 00:00:00 2001 From: alexmg Date: Sat, 24 Oct 2020 00:45:50 +1000 Subject: [PATCH] Fixes #82 - Allow the ContainerBuilderOptions to be provided when using the AutofacServiceProviderFactory --- appveyor.yml | 4 +- .../AutofacServiceProviderFactory.cs | 21 +++++++--- .../AutofacServiceProviderFactoryTests.cs | 40 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 74fcbde..76912b0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,9 +1,9 @@ image: Ubuntu -version: 7.0.2.{build} +version: 7.1.0.{build} dotnet_csproj: - version_prefix: '7.0.2' + version_prefix: '7.1.0' patch: true file: 'src\**\*.csproj' diff --git a/src/Autofac.Extensions.DependencyInjection/AutofacServiceProviderFactory.cs b/src/Autofac.Extensions.DependencyInjection/AutofacServiceProviderFactory.cs index 745a1bd..10735a9 100644 --- a/src/Autofac.Extensions.DependencyInjection/AutofacServiceProviderFactory.cs +++ b/src/Autofac.Extensions.DependencyInjection/AutofacServiceProviderFactory.cs @@ -24,6 +24,7 @@ // OTHER DEALINGS IN THE SOFTWARE. using System; +using Autofac.Builder; using Microsoft.Extensions.DependencyInjection; namespace Autofac.Extensions.DependencyInjection @@ -34,15 +35,25 @@ namespace Autofac.Extensions.DependencyInjection public class AutofacServiceProviderFactory : IServiceProviderFactory { private readonly Action _configurationAction; + private readonly ContainerBuildOptions _containerBuildOptions = ContainerBuildOptions.None; /// /// Initializes a new instance of the class. /// - /// Action on a that adds component registrations to the conatiner. - public AutofacServiceProviderFactory(Action configurationAction = null) - { + /// The container options to use when building the container. + /// Action on a that adds component registrations to the container. + public AutofacServiceProviderFactory( + ContainerBuildOptions containerBuildOptions, + Action configurationAction = null) + : this(configurationAction) => + _containerBuildOptions = containerBuildOptions; + + /// + /// Initializes a new instance of the class. + /// + /// Action on a that adds component registrations to the container.. + public AutofacServiceProviderFactory(Action configurationAction = null) => _configurationAction = configurationAction ?? (builder => { }); - } /// /// Creates a container builder from an . @@ -69,7 +80,7 @@ public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder) { if (containerBuilder == null) throw new ArgumentNullException(nameof(containerBuilder)); - var container = containerBuilder.Build(); + var container = containerBuilder.Build(_containerBuildOptions); return new AutofacServiceProvider(container); } diff --git a/test/Autofac.Extensions.DependencyInjection.Test/AutofacServiceProviderFactoryTests.cs b/test/Autofac.Extensions.DependencyInjection.Test/AutofacServiceProviderFactoryTests.cs index ad3fc39..a05ca8e 100644 --- a/test/Autofac.Extensions.DependencyInjection.Test/AutofacServiceProviderFactoryTests.cs +++ b/test/Autofac.Extensions.DependencyInjection.Test/AutofacServiceProviderFactoryTests.cs @@ -1,4 +1,5 @@ using System; +using Autofac.Builder; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -79,5 +80,44 @@ public void CreateServiceProviderReturnsAutofacServiceProvider() Assert.IsType(serviceProvider); } + + [Fact] + public void CreateServiceProviderUsesDefaultContainerBuildOptionsWhenNotProvided() + { + var factory = new AutofacServiceProviderFactory(); + var services = new ServiceCollection().AddSingleton("Foo"); + var builder = factory.CreateBuilder(services); + + var serviceProvider = factory.CreateServiceProvider(builder); + + Assert.NotNull(serviceProvider.GetService>()); + } + + [Fact] + public void CreateServiceProviderUsesContainerBuildOptionsWhenProvided() + { + var options = ContainerBuildOptions.ExcludeDefaultModules; + var factory = new AutofacServiceProviderFactory(options); + var services = new ServiceCollection().AddSingleton("Foo"); + var builder = factory.CreateBuilder(services); + + var serviceProvider = factory.CreateServiceProvider(builder); + + Assert.Null(serviceProvider.GetService>()); + } + + [Fact] + public void CanProvideContainerBuildOptionsAndConfigurationAction() + { + var factory = new AutofacServiceProviderFactory( + ContainerBuildOptions.ExcludeDefaultModules, + config => config.Register(c => "Foo")); + var builder = factory.CreateBuilder(new ServiceCollection()); + + var serviceProvider = factory.CreateServiceProvider(builder); + + Assert.NotNull(serviceProvider.GetService()); + Assert.Null(serviceProvider.GetService>()); + } } }