Skip to content

Commit

Permalink
Fixes #82 - Allow the ContainerBuilderOptions to be provided when usi…
Browse files Browse the repository at this point in the history
…ng the AutofacServiceProviderFactory
  • Loading branch information
alexmg committed Oct 23, 2020
1 parent 9bf6d85 commit 380b087
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using Autofac.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Autofac.Extensions.DependencyInjection
Expand All @@ -34,15 +35,25 @@ namespace Autofac.Extensions.DependencyInjection
public class AutofacServiceProviderFactory : IServiceProviderFactory<ContainerBuilder>
{
private readonly Action<ContainerBuilder> _configurationAction;
private readonly ContainerBuildOptions _containerBuildOptions = ContainerBuildOptions.None;

/// <summary>
/// Initializes a new instance of the <see cref="AutofacServiceProviderFactory"/> class.
/// </summary>
/// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/> that adds component registrations to the conatiner.</param>
public AutofacServiceProviderFactory(Action<ContainerBuilder> configurationAction = null)
{
/// <param name="containerBuildOptions">The container options to use when building the container.</param>
/// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/> that adds component registrations to the container.</param>
public AutofacServiceProviderFactory(
ContainerBuildOptions containerBuildOptions,
Action<ContainerBuilder> configurationAction = null)
: this(configurationAction) =>
_containerBuildOptions = containerBuildOptions;

/// <summary>
/// Initializes a new instance of the <see cref="AutofacServiceProviderFactory"/> class.
/// </summary>
/// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/> that adds component registrations to the container..</param>
public AutofacServiceProviderFactory(Action<ContainerBuilder> configurationAction = null) =>
_configurationAction = configurationAction ?? (builder => { });
}

/// <summary>
/// Creates a container builder from an <see cref="IServiceCollection" />.
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Autofac.Builder;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

Expand Down Expand Up @@ -79,5 +80,44 @@ public void CreateServiceProviderReturnsAutofacServiceProvider()

Assert.IsType<AutofacServiceProvider>(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<Lazy<string>>());
}

[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<Lazy<string>>());
}

[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<string>());
Assert.Null(serviceProvider.GetService<Lazy<string>>());
}
}
}

0 comments on commit 380b087

Please sign in to comment.