Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/LightInject.Tests/AssemblyScannerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,32 @@ public void Register_AssemblyWithFunc_CallsAssemblyScanner()
scannerMock.Assert(a => a.Scan(typeof(IFoo).Assembly, The<IServiceRegistry>.IsAnyValue, The<Func<ILifetime>>.IsAnyValue, The<Func<Type, Type, bool>>.IsAnyValue), Invoked.Once);
}

[Fact]
public void Register_Assembly_CallsAssemblyScannerWhenAssemblyScanningEnabled()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the convention used to name the unit tests in this project so I will welcome suggestions if you can think of a better name ;)

{
var scannerMock = new AssemblyScannerMock();
var compositionRootExtractorMock = new TypeExtractorMock();
compositionRootExtractorMock.Arrange(c => c.Execute(The<Assembly>.IsAnyValue)).Returns(Type.EmptyTypes);

var serviceContainer = new ServiceContainer(new ContainerOptions { ScanAssembliesWhenRegistrationNotFound = true });
serviceContainer.CompositionRootTypeExtractor = compositionRootExtractorMock;
serviceContainer.AssemblyScanner = scannerMock;
scannerMock.Assert(a => a.Scan(typeof(IFoo).Assembly, The<IServiceRegistry>.IsAnyValue, The<Func<ILifetime>>.IsAnyValue, The<Func<Type, Type, bool>>.IsAnyValue), Invoked.Never);
}

[Fact]
public void Register_Assembly_DoesNotCallsAssemblyScannerWhenAssemblyScanningDisabled()
{
var scannerMock = new AssemblyScannerMock();
var compositionRootExtractorMock = new TypeExtractorMock();
compositionRootExtractorMock.Arrange(c => c.Execute(The<Assembly>.IsAnyValue)).Returns(Type.EmptyTypes);

var serviceContainer = new ServiceContainer(new ContainerOptions { ScanAssembliesWhenRegistrationNotFound = false });
serviceContainer.CompositionRootTypeExtractor = compositionRootExtractorMock;
serviceContainer.AssemblyScanner = scannerMock;
scannerMock.Assert(a => a.Scan(typeof(IFoo).Assembly, The<IServiceRegistry>.IsAnyValue, The<Func<ILifetime>>.IsAnyValue, The<Func<Type, Type, bool>>.IsAnyValue), Invoked.Never);
}

[Fact]
public void Register_AssemblyWithFuncAndLifeCycle_CallsAssemblyScanner()
{
Expand Down
21 changes: 15 additions & 6 deletions src/LightInject/LightInject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,7 @@ public ContainerOptions()
{
EnableVariance = true;
EnablePropertyInjection = true;
ScanAssembliesWhenRegistrationNotFound = true;
LogFactory = t => message => { };
}

Expand Down Expand Up @@ -1788,6 +1789,14 @@ public ContainerOptions()
/// </remarks>
public bool EnablePropertyInjection { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to scan assemblies when a type is not registered.
/// </summary>
/// <remarks>
/// The default value is true.
/// </remarks>
public bool ScanAssembliesWhenRegistrationNotFound { get; set; }

private static ContainerOptions CreateDefaultContainerOptions()
{
return new ContainerOptions();
Expand Down Expand Up @@ -1877,7 +1886,7 @@ public ServiceContainer(ContainerOptions options)
var concreteTypeExtractor = new CachedTypeExtractor(new ConcreteTypeExtractor());
CompositionRootTypeExtractor = new CachedTypeExtractor(new CompositionRootTypeExtractor(new CompositionRootAttributeExtractor()));
CompositionRootExecutor = new CompositionRootExecutor(this, type => (ICompositionRoot)Activator.CreateInstance(type));
PropertyDependencySelector = options.EnablePropertyInjection
PropertyDependencySelector = options.EnablePropertyInjection
? (IPropertyDependencySelector)new PropertyDependencySelector(new PropertySelector())
: new PropertyDependencyDisabler();
GenericArgumentMapper = new GenericArgumentMapper();
Expand Down Expand Up @@ -3035,15 +3044,15 @@ private Action<IEmitter> GetEmitMethod(Type serviceType, string serviceName)
emitMethod = TryGetFallbackEmitMethod(serviceType, serviceName);
}

if (emitMethod == null)
if (emitMethod == null && options.ScanAssembliesWhenRegistrationNotFound)
{
AssemblyScanner.Scan(serviceType.GetTypeInfo().Assembly, this);
emitMethod = GetRegisteredEmitMethod(serviceType, serviceName);
}

if (emitMethod == null)
{
emitMethod = TryGetFallbackEmitMethod(serviceType, serviceName);
if (emitMethod == null)
{
emitMethod = TryGetFallbackEmitMethod(serviceType, serviceName);
}
}

return CreateEmitMethodWrapper(emitMethod, serviceType, serviceName);
Expand Down