|
| 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 | +} |
0 commit comments