diff --git a/src/LightInject.Tests/AssemblyScannerTests.cs b/src/LightInject.Tests/AssemblyScannerTests.cs index 87765532..acb89bd8 100644 --- a/src/LightInject.Tests/AssemblyScannerTests.cs +++ b/src/LightInject.Tests/AssemblyScannerTests.cs @@ -297,6 +297,32 @@ public void Register_AssemblyWithFunc_CallsAssemblyScanner() scannerMock.Assert(a => a.Scan(typeof(IFoo).Assembly, The.IsAnyValue, The>.IsAnyValue, The>.IsAnyValue), Invoked.Once); } + [Fact] + public void Register_Assembly_CallsAssemblyScannerWhenAssemblyScanningEnabled() + { + var scannerMock = new AssemblyScannerMock(); + var compositionRootExtractorMock = new TypeExtractorMock(); + compositionRootExtractorMock.Arrange(c => c.Execute(The.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.IsAnyValue, The>.IsAnyValue, The>.IsAnyValue), Invoked.Never); + } + + [Fact] + public void Register_Assembly_DoesNotCallsAssemblyScannerWhenAssemblyScanningDisabled() + { + var scannerMock = new AssemblyScannerMock(); + var compositionRootExtractorMock = new TypeExtractorMock(); + compositionRootExtractorMock.Arrange(c => c.Execute(The.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.IsAnyValue, The>.IsAnyValue, The>.IsAnyValue), Invoked.Never); + } + [Fact] public void Register_AssemblyWithFuncAndLifeCycle_CallsAssemblyScanner() { diff --git a/src/LightInject/LightInject.cs b/src/LightInject/LightInject.cs index 1e11f964..d638de8a 100644 --- a/src/LightInject/LightInject.cs +++ b/src/LightInject/LightInject.cs @@ -1759,6 +1759,7 @@ public ContainerOptions() { EnableVariance = true; EnablePropertyInjection = true; + ScanAssembliesWhenRegistrationNotFound = true; LogFactory = t => message => { }; } @@ -1788,6 +1789,14 @@ public ContainerOptions() /// public bool EnablePropertyInjection { get; set; } + /// + /// Gets or sets a value indicating whether to scan assemblies when a type is not registered. + /// + /// + /// The default value is true. + /// + public bool ScanAssembliesWhenRegistrationNotFound { get; set; } + private static ContainerOptions CreateDefaultContainerOptions() { return new ContainerOptions(); @@ -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(); @@ -3035,15 +3044,15 @@ private Action 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);