Skip to content

Commit

Permalink
Merge pull request #28 from henrihs/bugfix/align-to-lightinject
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
seesharper authored Aug 22, 2018
2 parents 2442567 + 9ee7a91 commit 8fbd101
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
public class LightInjectSpecificationTests : DependencyInjectionSpecificationTests
{
protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection)
{
var container = new ServiceContainer(new ContainerOptions() { EnablePropertyInjection = false, DefaultServiceSelector = services => services.Last() });
{
var container = new ServiceContainer(new ContainerOptions() { EnablePropertyInjection = false, DefaultServiceSelector = services => services.SingleOrDefault(string.IsNullOrWhiteSpace) ?? services.Last() });
return container.CreateServiceProvider(serviceCollection);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,47 @@ public void ShouldCreateServiceProvider()

Assert.IsAssignableFrom<IServiceContainer>(container);
}

[Fact]
public void ShouldDisposePerContainerServicesWhenProviderIsDisposed()
{
var serviceCollection = new ServiceCollection();
var factory = new LightInjectServiceProviderFactory();
var container = factory.CreateBuilder(serviceCollection);
container.Register<DisposableFoo>(new PerContainerLifetime());
using (var provider = (IDisposable)container.CreateServiceProvider(serviceCollection))
{
((IServiceProvider)provider).GetService<DisposableFoo>();
}

Assert.True(DisposableFoo.IsDisposed);
}

[Fact]
public void ShouldPickServiceWithoutServiceNameAsDefaultIfRegistered()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<string>(p => "42");
var factory = new LightInjectServiceProviderFactory();
var container = factory.CreateBuilder(serviceCollection);

container.Register<string>(f => "84");
var provider = container.CreateServiceProvider(serviceCollection);
var value = provider.GetService<string>();

Assert.Equal("84", value);
}



public class DisposableFoo : IDisposable
{
public static bool IsDisposed;

public void Dispose()
{
IsDisposed = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static class DependencyInjectionContainerExtensions
public static IServiceProvider CreateServiceProvider(this IServiceContainer container, IServiceCollection serviceCollection)
{
var rootScope = container.BeginScope();
rootScope.Completed += (a, s) => container.Dispose();
container.Register<IServiceProvider>(factory => new LightInjectServiceProvider(container), new PerRootScopeLifetime(rootScope));
container.Register<IServiceScopeFactory>(factory => new LightInjectServiceScopeFactory(container), new PerRootScopeLifetime(rootScope));
RegisterServices(container, rootScope, serviceCollection);
Expand Down Expand Up @@ -182,7 +183,7 @@ public LightInjectServiceProviderFactory()
/// <param name="options">The <see cref="ContainerOptions"/> to be used when creating the <see cref="ServiceContainer"/>.</param>
public LightInjectServiceProviderFactory(ContainerOptions options)
{
options.DefaultServiceSelector = serviceNames => serviceNames.Last();
options.DefaultServiceSelector = serviceNames => serviceNames.SingleOrDefault(string.IsNullOrWhiteSpace) ?? serviceNames.Last();
options.EnablePropertyInjection = false;
this.options = options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<RepositoryType>git</RepositoryType>
<PackageTags>Ioc Dependency-Injection Inversion-of-Control LightInject ASP.NET Entity-Framework</PackageTags>
<DebugType>Full</DebugType>
<Version>2.0.7</Version>
<Version>2.0.8</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<DocumentationFile>bin\Debug\netstandard2.0\LightInject.Microsoft.DependencyInjection.xml</DocumentationFile>
Expand Down

0 comments on commit 8fbd101

Please sign in to comment.