Skip to content

Commit 09f0f07

Browse files
committed
Test showing scoped dependency bug still in 5.11
1 parent f213a66 commit 09f0f07

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

UnitTests/ScopedDepencencyTests.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System.Collections.Generic;
3+
using Unity.Microsoft.DependencyInjection;
4+
using Xunit;
5+
6+
namespace UnitTests
7+
{
8+
public class ScopedDepencencyTests
9+
{
10+
[Fact]
11+
public void ScopedDependencyFromTransientFactoryNotSharedAcrossScopes()
12+
{
13+
// Arrange
14+
var collection = new TestServiceCollection()
15+
.AddTransient<ITransient>(CreateTransientFactory)
16+
.AddScoped<IScoped, Scoped>();
17+
18+
var provider = collection.BuildServiceProvider();
19+
20+
// Act
21+
ITransient transient1 = null;
22+
ITransient transient2a = null;
23+
ITransient transient2b = null;
24+
25+
using (var scope1 = provider.CreateScope())
26+
{
27+
transient1 = scope1.ServiceProvider.GetService<ITransient>();
28+
}
29+
30+
using (var scope2 = provider.CreateScope())
31+
{
32+
transient2a = scope2.ServiceProvider.GetService<ITransient>();
33+
transient2b = scope2.ServiceProvider.GetService<ITransient>();
34+
}
35+
36+
// Assert
37+
Assert.NotSame(transient1, transient2a);
38+
Assert.NotSame(transient2a, transient2b);
39+
Assert.NotSame(transient1.ScopedDependency, transient2a.ScopedDependency);
40+
Assert.Same(transient2a.ScopedDependency, transient2b.ScopedDependency);
41+
}
42+
43+
private ITransient CreateTransientFactory(System.IServiceProvider provider)
44+
{
45+
return provider.GetRequiredService<Transient>();
46+
}
47+
48+
public interface ITransient
49+
{
50+
IScoped ScopedDependency { get; }
51+
}
52+
53+
public class Transient : ITransient
54+
{
55+
public Transient(IScoped scoped)
56+
{
57+
ScopedDependency = scoped;
58+
}
59+
60+
public IScoped ScopedDependency { get; }
61+
}
62+
63+
public interface IScoped { }
64+
65+
public class Scoped : IScoped
66+
{
67+
}
68+
69+
}
70+
71+
internal class TestServiceCollection : List<ServiceDescriptor>, IServiceCollection
72+
{
73+
}
74+
}

UnitTests/UnitTests.csproj

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
12+
<PackageReference Include="Unity.Container" Version="5.11.4" />
13+
<PackageReference Include="Unity.Microsoft.DependencyInjection" Version="5.11.0" />
14+
<PackageReference Include="xunit" Version="2.4.0" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
16+
<PackageReference Include="coverlet.collector" Version="1.0.1" />
17+
</ItemGroup>
18+
19+
</Project>

package.sln

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27004.2005
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29418.71
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.DependencyInjection", "src\Unity.Microsoft.DependencyInjection.csproj", "{520251C7-14E6-434F-A26E-67E9762635BD}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Microsoft.DependencyInjection", "src\Unity.Microsoft.DependencyInjection.csproj", "{520251C7-14E6-434F-A26E-67E9762635BD}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{DA61FEC3-1E17-4DCF-AC19-A6482A547741}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjection.Tests", "tests\Unity.Microsoft.DependencyInjection.Tests.csproj", "{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unity.Microsoft.DependencyInjection.Tests", "tests\Unity.Microsoft.DependencyInjection.Tests.csproj", "{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}"
11+
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -23,6 +25,10 @@ Global
2325
{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
2426
{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
2527
{FCC3DAA4-9136-4C55-926D-7DBD5BEDE8D8}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{04C6DC73-DC25-4992-A2B2-B87FD5C44EB0}.Release|Any CPU.Build.0 = Release|Any CPU
2632
EndGlobalSection
2733
GlobalSection(SolutionProperties) = preSolution
2834
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)