diff --git a/src/DryIoc/Container.cs b/src/DryIoc/Container.cs index c0b084b4..6b2be402 100644 --- a/src/DryIoc/Container.cs +++ b/src/DryIoc/Container.cs @@ -4724,7 +4724,6 @@ public static Expression GetRootOrSelfExpr(Request request) => request.Reuse is CurrentScopeReuse == false && request.DirectParent.IsSingletonOrDependencyOfSingleton && request.Rules.ThrowIfDependencyHasShorterReuseLifespan // see the #378 - // && !request.OpensResolutionScope && !request.OpensResolutionScopeUpToResolutionCall() ? RootOrSelfExpr : FactoryDelegateCompiler.ResolverContextParamExpr; @@ -4822,11 +4821,7 @@ public static IScope GetNamedScope(this IResolverContext r, object name, bool th } if (s == null) - { - if (throwIfNotFound) - Throw.It(Error.NoMatchedScopeFound, name, currentScope); - return null; - } + return throwIfNotFound ? Throw.For(Error.NoMatchedScopeFound, name, currentScope) : null; if (s.IsDisposed) { @@ -5131,7 +5126,7 @@ private static Expression GetArrayExpression(Request request) var itemRequest = request.Push(itemInfo); // For the required service type (not a wrapper) we at least looking at the unwrapped type, so we may check that type factory condition, - // or going to resolve the nested wrapper and Store the unwrapped factory in the request but did not check it until we down the wrappers chain with all available information + // or going to resolve the nested wrapper and Store the unwrapped factory in the request but did not check it until we down the wrappers chain with all the available information var factory = requiredItemType == itemType ? itemRequest.MatchGeneratedFactoryByReuseAndConditionOrNull(item.Factory) : container.ResolveFactory(itemRequest.WithWrappedServiceFactory(item.Factory)); @@ -9380,8 +9375,7 @@ public static bool MatchGeneratedFactory(this Request r, Factory f) => /// Matching things public static Factory MatchGeneratedFactoryByReuseAndConditionOrNull(this Request r, Factory f) { - var reuse = f.Reuse; - if (reuse != null && !reuse.CanApply(r)) + if (!f.Setup.OpenResolutionScope && !r.MatchFactoryReuse(f)) return null; var condition = f.Setup.Condition; diff --git a/test/DryIoc.IssuesTests/GHIssue559_Possible_inconsistent_behaviour.cs b/test/DryIoc.IssuesTests/GHIssue559_Possible_inconsistent_behaviour.cs index 244d9655..43bb38a2 100644 --- a/test/DryIoc.IssuesTests/GHIssue559_Possible_inconsistent_behaviour.cs +++ b/test/DryIoc.IssuesTests/GHIssue559_Possible_inconsistent_behaviour.cs @@ -9,12 +9,13 @@ public class GHIssue559_Possible_inconsistent_behaviour : ITest { public int Run() { - Test1(); - return 1; + Test_scoped_opening_scope(); + Test_singleton_opening_scope(); + return 2; } [Test] - public void Test1() + public void Test_scoped_opening_scope() { var container = new Container(); container.Register(Reuse.Scoped, setup: Setup.With(openResolutionScope: true)); @@ -23,7 +24,20 @@ public void Test1() Assert.IsNotNull(foo); var actual = container.Resolve>(); - Assert.AreEqual(0, actual.Count()); // todo: @fixme + Assert.AreEqual(1, actual.Count()); + } + + [Test] + public void Test_singleton_opening_scope() + { + var container = new Container(); + container.Register(Reuse.Singleton, setup: Setup.With(openResolutionScope: true)); + + var foo1 = container.Resolve(); + Assert.IsNotNull(foo1); + + var foo2 = container.Resolve(); + Assert.AreSame(foo1, foo2); } public class Foo {} diff --git a/test/DryIoc.TestRunner/Program.cs b/test/DryIoc.TestRunner/Program.cs index 4d176af7..ebfdb26b 100644 --- a/test/DryIoc.TestRunner/Program.cs +++ b/test/DryIoc.TestRunner/Program.cs @@ -11,7 +11,6 @@ public static void Main() RunAllTests(); // new GHIssue559_Possible_inconsistent_behaviour().Run(); - // new GHIssue557_WithFactorySelector_allows_to_Resolve_the_keyed_service_as_non_keyed().Run(); // new GHIssue555_ConcreteTypeDynamicRegistrations_is_not_working_with_MicrosoftDependencyInjectionRules().Run(); // new GHIssue554_System_NullReferenceException_Object_reference_not_set_to_an_instance_of_an_object().Run(); @@ -74,6 +73,7 @@ void Run(Func run, string name = null) new GHIssue554_System_NullReferenceException_Object_reference_not_set_to_an_instance_of_an_object(), new GHIssue555_ConcreteTypeDynamicRegistrations_is_not_working_with_MicrosoftDependencyInjectionRules(), new GHIssue557_WithFactorySelector_allows_to_Resolve_the_keyed_service_as_non_keyed(), + new GHIssue559_Possible_inconsistent_behaviour(), }; // Parallel.ForEach(tests, x => Run(x.Run)); // todo: @perf enable and test when more tests are added