|
| 1 | +using NUnit.Framework; |
| 2 | +using System; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using DryIoc.Microsoft.DependencyInjection; |
| 6 | + |
| 7 | +namespace DryIoc.IssuesTests; |
| 8 | + |
| 9 | +[TestFixture] |
| 10 | +public class GHIssue678_Scope_is_lost_in_diposable_service : ITest |
| 11 | +{ |
| 12 | + public int Run() |
| 13 | + { |
| 14 | + Original_case(); |
| 15 | + return 1; |
| 16 | + } |
| 17 | + |
| 18 | + [Test] |
| 19 | + public void Original_case() |
| 20 | + { |
| 21 | + IServiceProvider serviceProvider = null; |
| 22 | + |
| 23 | + var host = Host.CreateDefaultBuilder() |
| 24 | + .ConfigureServices(services => |
| 25 | + { |
| 26 | + serviceProvider = new Container( |
| 27 | + // NOTE: You don't need to specify the rules here, |
| 28 | + // because this rule is the part of `WithDependencyInjectionAdapter` configuration |
| 29 | + // |
| 30 | + // rules => rules.With(FactoryMethod.ConstructorWithResolvableArguments) |
| 31 | + ) |
| 32 | + .WithDependencyInjectionAdapter(services) |
| 33 | + // NOTE: you need this line (for now) to obtain the Container from the DryIocServiceProvider |
| 34 | + // in the DryIoc.MS.DI v8.0.0-preview-04. |
| 35 | + // I will add the new ConfigureServiceProvider overload for the DryIocServiceProvider into the later version, |
| 36 | + // so you don't need to change your code. |
| 37 | + .Container |
| 38 | + .ConfigureServiceProvider<CompositionRoot>(); |
| 39 | + }) |
| 40 | + .Build(); |
| 41 | + |
| 42 | + using var scope = serviceProvider.CreateScope(); |
| 43 | + |
| 44 | + var service = scope.ServiceProvider.GetRequiredService<ServiceA>(); |
| 45 | + service.Do(); |
| 46 | + } |
| 47 | + |
| 48 | + public class CompositionRoot |
| 49 | + { |
| 50 | + public CompositionRoot(IRegistrator registrator) |
| 51 | + { |
| 52 | + registrator.Register<Context>(reuse: Reuse.Scoped); |
| 53 | + registrator.Register<Strategy>(); |
| 54 | + registrator.Register<ServiceC<Strategy>>(); |
| 55 | + registrator.Register<ServiceB>(); |
| 56 | + registrator.Register<ServiceA>(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public class Context |
| 61 | + { |
| 62 | + public string Value { get; set; } |
| 63 | + } |
| 64 | + |
| 65 | + public class Strategy |
| 66 | + { |
| 67 | + public Strategy(Context context, IResolver resolver) |
| 68 | + { |
| 69 | + var resolvedContext = resolver.Resolve<Context>(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public class ServiceC<TContext> : IDisposable |
| 74 | + { |
| 75 | + public ServiceC(Strategy strategy, Context context) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + public void Dispose() |
| 80 | + { |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public class ServiceB |
| 85 | + { |
| 86 | + private readonly IContainer _container; |
| 87 | + private readonly Context _context; |
| 88 | + |
| 89 | + public ServiceB(IContainer container, Context context) |
| 90 | + { |
| 91 | + _container = container; |
| 92 | + _context = context; |
| 93 | + } |
| 94 | + |
| 95 | + public void Do() |
| 96 | + { |
| 97 | + using var scope = _container.OpenScope(); |
| 98 | + scope.Use(_context); |
| 99 | + |
| 100 | + var context = scope.Resolve<Context>(); |
| 101 | + |
| 102 | + // here context.value is "value" |
| 103 | + var anotherContext = scope.Resolve<Strategy>(); |
| 104 | + |
| 105 | + // here context.value is null |
| 106 | + // here resolvedContext.value is "value" - resolvedContext is resolved from injected IResolver |
| 107 | + using var serviceC = scope.Resolve<ServiceC<Strategy>>(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public class ServiceA |
| 112 | + { |
| 113 | + private readonly IContainer _container; |
| 114 | + private readonly Context _context; |
| 115 | + private readonly ServiceB _serviceB; |
| 116 | + |
| 117 | + public ServiceA(IContainer container, Context context, ServiceB serviceB) |
| 118 | + { |
| 119 | + _container = container; |
| 120 | + _context = context; |
| 121 | + _serviceB = serviceB; |
| 122 | + } |
| 123 | + |
| 124 | + public void Do() |
| 125 | + { |
| 126 | + using var serviceC = _container.Resolve<ServiceC<Strategy>>(); // this cause the issue, please comment this line to resolve problem |
| 127 | + |
| 128 | + _context.Value = "value"; |
| 129 | + |
| 130 | + _serviceB.Do(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments