Skip to content

Commit d950c53

Browse files
committed
Fixed #29
1 parent f3ae8e2 commit d950c53

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed

src/Configuration.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ internal static void Register(this IUnityContainer container,
5252
serviceDescriptor.GetLifetime(lifetime),
5353
new InjectionFactory(scope =>
5454
{
55-
var serviceProvider = serviceDescriptor.Lifetime == ServiceLifetime.Scoped
56-
? scope.Resolve<IServiceProvider>()
57-
: container.Resolve<IServiceProvider>();
58-
var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
55+
var instance = serviceDescriptor.ImplementationFactory(scope.Resolve<IServiceProvider>());
5956
return instance;
6057
}));
6158
}

tests/GitHub.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using System;
3+
using System.Net.Http;
4+
using System.Reflection;
5+
using Xunit;
6+
7+
namespace Unity.Microsoft.DependencyInjection.Tests
8+
{
9+
public class GitHubIssues
10+
{
11+
public interface IScopedService
12+
{
13+
IServiceProvider ServiceProvider { get; }
14+
}
15+
16+
public class ScopedService : IScopedService
17+
{
18+
public ScopedService(IServiceProvider serviceProvider)
19+
{
20+
ServiceProvider = serviceProvider;
21+
}
22+
public IServiceProvider ServiceProvider { get; }
23+
}
24+
25+
private interface ITransientService
26+
{
27+
IScopedService ScopedService { get; }
28+
}
29+
30+
public class TransientService : ITransientService
31+
{
32+
public TransientService(IScopedService scopedService)
33+
{
34+
ScopedService = scopedService;
35+
}
36+
public IScopedService ScopedService { get; }
37+
}
38+
39+
[Fact]
40+
public void Issue_29_Type()
41+
{
42+
var sc = new ServiceCollection();
43+
44+
sc.AddTransient<ITransientService, TransientService>();
45+
sc.AddScoped<IScopedService, ScopedService>();
46+
var sp = sc.BuildServiceProvider(new UnityContainer());
47+
48+
Assert.Same(sp, sp.GetRequiredService<IScopedService>().ServiceProvider);
49+
Assert.Same(sp, sp.GetRequiredService<ITransientService>().ScopedService.ServiceProvider);
50+
var ssf = sp.GetRequiredService<IServiceScopeFactory>();
51+
using (var scope = ssf.CreateScope())
52+
{
53+
var scopedSp = scope.ServiceProvider;
54+
var scoped = scopedSp.GetRequiredService<IScopedService>();
55+
var transient = scopedSp.GetRequiredService<ITransientService>();
56+
57+
Assert.Same(scopedSp, scoped.ServiceProvider);
58+
Assert.Same(scopedSp, transient.ScopedService.ServiceProvider);
59+
}
60+
}
61+
62+
63+
[Fact]
64+
public void Issue_29_Factory()
65+
{
66+
var sc = new ServiceCollection();
67+
68+
sc.AddTransient<ITransientService>(o => new TransientService(o.GetRequiredService<IScopedService>()));
69+
sc.AddScoped<IScopedService, ScopedService>();
70+
var sp = sc.BuildServiceProvider(new UnityContainer());
71+
72+
Assert.Same(sp, sp.GetRequiredService<IScopedService>().ServiceProvider);
73+
Assert.Same(sp, sp.GetRequiredService<ITransientService>().ScopedService.ServiceProvider);
74+
var ssf = sp.GetRequiredService<IServiceScopeFactory>();
75+
using (var scope = ssf.CreateScope())
76+
{
77+
var scopedSp = scope.ServiceProvider;
78+
var scoped = scopedSp.GetRequiredService<IScopedService>();
79+
var transient = scopedSp.GetRequiredService<ITransientService>();
80+
81+
Assert.Same(scopedSp, scoped.ServiceProvider);
82+
Assert.Same(scopedSp, transient.ScopedService.ServiceProvider);
83+
}
84+
}
85+
86+
87+
[Fact]
88+
public void Test2_failing()
89+
{
90+
91+
var serviceCollection = new ServiceCollection();
92+
93+
94+
serviceCollection.AddHttpClient();
95+
96+
IUnityContainer container = new UnityContainer().CreateChildContainer();
97+
98+
var factory = new ServiceProviderFactory(c => c.UnityContainer = container);
99+
100+
var sp = factory.CreateServiceProvider(serviceCollection);
101+
var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
102+
using (var scope = scopeFactory.CreateScope())
103+
{
104+
var httpFactory = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>();
105+
106+
var sp1 = httpFactory.GetType().GetField("_services", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(httpFactory) as IServiceProvider;
107+
var c1 = sp1.GetType().GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sp1);
108+
Assert.NotNull(c1); // "In scope"
109+
110+
}
111+
112+
{
113+
var httpFactory = sp.GetRequiredService<IHttpClientFactory>();
114+
var sp1 = httpFactory.GetType().GetField("_services", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(httpFactory) as IServiceProvider;
115+
var c1 = sp1.GetType().GetField("_container", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(sp1);
116+
Assert.NotNull(c1); // "after scope"
117+
}
118+
119+
}
120+
}
121+
}

tests/Microsoft.DependencyInjection.Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
1011
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
1112
<PackageReference Include="xunit" Version="2.4.0" />
1213
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
@@ -27,4 +28,13 @@
2728
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="2.1.1" />
2829
</ItemGroup>
2930

31+
<ItemGroup>
32+
<Reference Include="Microsoft.Extensions.DependencyInjection">
33+
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.dependencyinjection\2.1.1\lib\netcoreapp2.0\Microsoft.Extensions.DependencyInjection.dll</HintPath>
34+
</Reference>
35+
<Reference Include="Microsoft.Extensions.Http">
36+
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.http\2.1.1\lib\netstandard2.0\Microsoft.Extensions.Http.dll</HintPath>
37+
</Reference>
38+
</ItemGroup>
39+
3040
</Project>

0 commit comments

Comments
 (0)