Skip to content

Commit

Permalink
Releasing 5.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Dec 6, 2019
1 parent fcca416 commit 3b4b03c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>5.11.0</Version>
<Version>5.11.1</Version>
<PackageReleaseNotes>Bug fixes, dependency updates and minor performance optimizations </PackageReleaseNotes>
</PropertyGroup>

Expand Down
4 changes: 1 addition & 3 deletions src/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ internal static void Register(this IUnityContainer container,
null,
scope =>
{
var serviceProvider = serviceDescriptor.Lifetime == ServiceLifetime.Scoped
? scope.Resolve<IServiceProvider>()
: container.Resolve<IServiceProvider>();
var serviceProvider = scope.Resolve<IServiceProvider>();
var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
return instance;
},
Expand Down
123 changes: 100 additions & 23 deletions tests/ScopedDepencencyTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using Unity;
using Unity.Microsoft.DependencyInjection;
using Xunit;

namespace UnitTests
namespace Unity.Microsoft.DependencyInjection.Unit.Tests
{
public class ScopedDepencencyTests
{
[Fact]
public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes()
public void aspnet_Extensions_issues_1301()
{
var services = new TestServiceCollection()
.AddSingleton<Foo>();

var provider = services.BuildServiceProvider();

IServiceProvider scopedSp1 = null;
IServiceProvider scopedSp2 = null;
Foo foo1 = null;
Foo foo2 = null;

using (var scope1 = provider.CreateScope())
{
scopedSp1 = scope1.ServiceProvider;
foo1 = scope1.ServiceProvider.GetRequiredService<Foo>();
}

using (var scope2 = provider.CreateScope())
{
scopedSp2 = scope2.ServiceProvider;
foo2 = scope2.ServiceProvider.GetRequiredService<Foo>();
}

Assert.Equal(foo1.ServiceProvider, foo2.ServiceProvider);
Assert.NotEqual(foo1.ServiceProvider, scopedSp1);
Assert.NotEqual(foo2.ServiceProvider, scopedSp2);
}

[Fact]
public void ScopedDependencyFromFactoryNotSharedAcrossScopes()
{
// Arrange
var collection = new TestServiceCollection()
Expand All @@ -18,54 +50,99 @@ public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes()
var provider = collection.BuildServiceProvider();

// Act
ITransient transient1 = null;
ITransient transient2a = null;
ITransient transient1a = null;
ITransient transient1b = null;
ITransient transient2b = null;

using (var scope1 = provider.CreateScope())
{
transient1 = scope1.ServiceProvider.GetService<ITransient>();
transient1a = scope1.ServiceProvider.GetService<ITransient>();
}

using (var scope2 = provider.CreateScope())
{
transient2a = scope2.ServiceProvider.GetService<ITransient>();
transient1b = scope2.ServiceProvider.GetService<ITransient>();
transient2b = scope2.ServiceProvider.GetService<ITransient>();
}

// Assert
Assert.NotSame(transient1, transient2a);
Assert.NotSame(transient2a, transient2b);
Assert.NotSame(transient1.ScopedDependency, transient2a.ScopedDependency);
Assert.Same(transient2a.ScopedDependency, transient2b.ScopedDependency);
Assert.NotSame(transient1a, transient1b);
Assert.NotSame(transient1b, transient2b);
Assert.NotSame(transient1a.ScopedDependency, transient1b.ScopedDependency);
Assert.Same(transient1b.ScopedDependency, transient2b.ScopedDependency);
}

private ITransient CreateTransientFactory(System.IServiceProvider provider)
[Fact]
public void ScopedDependencyFromTransientNotSharedAcrossScopes()
{
return provider.GetRequiredService<Transient>();
// Arrange
var collection = new TestServiceCollection()
.AddTransient<ITransient, Transient>()
.AddScoped<IScoped, Scoped>();

var provider = collection.BuildServiceProvider();

// Act
ITransient transient1a = null;
ITransient transient1b = null;
ITransient transient2b = null;

using (var scope1 = provider.CreateScope())
{
transient1a = scope1.ServiceProvider.GetService<ITransient>();
}

using (var scope2 = provider.CreateScope())
{
transient1b = scope2.ServiceProvider.GetService<ITransient>();
transient2b = scope2.ServiceProvider.GetService<ITransient>();
}

// Assert
Assert.NotSame(transient1a, transient1b);
Assert.NotSame(transient1b, transient2b);
Assert.NotSame(transient1a.ScopedDependency, transient1b.ScopedDependency);
Assert.Same(transient1b.ScopedDependency, transient2b.ScopedDependency);
}

public interface ITransient
private ITransient CreateTransientFactory(IServiceProvider provider)
{
IScoped ScopedDependency { get; }
return provider.GetRequiredService<Transient>();
}
}

public class Transient : ITransient
public class Foo
{
public Foo(IServiceProvider sp)
{
public Transient(IScoped scoped)
{
ScopedDependency = scoped;
}

public IScoped ScopedDependency { get; }
ServiceProvider = sp;
}

public interface IScoped { }
public IServiceProvider ServiceProvider { get; }
}

public interface ITransient
{
IScoped ScopedDependency { get; }
}

public class Transient : ITransient
{
string ID { get; } = Guid.NewGuid().ToString();

public class Scoped : IScoped
public Transient(IScoped scoped)
{
ScopedDependency = scoped;
}

public IScoped ScopedDependency { get; }
}

public interface IScoped { }

public class Scoped : IScoped
{
string ID { get; } = Guid.NewGuid().ToString();
}

internal class TestServiceCollection : List<ServiceDescriptor>, IServiceCollection
Expand Down
3 changes: 1 addition & 2 deletions tests/UnityDependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
using Microsoft.Extensions.DependencyInjection.Specification;
using Microsoft.Extensions.DependencyInjection.Specification.Fakes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Unity.Microsoft.DependencyInjection.Tests
namespace Unity.Microsoft.DependencyInjection.Specification.Tests
{
public class Tests : DependencyInjectionSpecificationTests
{
Expand Down

0 comments on commit 3b4b03c

Please sign in to comment.