From 83da3c2ea0ef5308ae9b379e215892392624b267 Mon Sep 17 00:00:00 2001 From: dadhi Date: Tue, 9 Aug 2022 18:28:12 +0200 Subject: [PATCH] fixed: #516 --- src/DryIoc/Container.cs | 2 +- ...to_Scoped_base_should_not_work_but_does.cs | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/DryIoc.IssuesTests/GHIssue516_Singleton_Decorator_to_Scoped_base_should_not_work_but_does.cs diff --git a/src/DryIoc/Container.cs b/src/DryIoc/Container.cs index d70c5ed5d..e9f1abdeb 100644 --- a/src/DryIoc/Container.cs +++ b/src/DryIoc/Container.cs @@ -9892,7 +9892,7 @@ public Request WithResolvedFactory(Factory factory, scopedOrSingleton && p.Reuse is SingletonReuse || p.FactoryType == FactoryType.Wrapper && p._actualServiceType.IsFunc()) checkCaptiveDependency = false; // stop the check - else if (p.FactoryType == FactoryType.Service && p.ReuseLifespan > reuseLifespan) + else if (p.FactoryType != FactoryType.Wrapper && p.ReuseLifespan > reuseLifespan) Throw.It(Error.DependencyHasShorterReuseLifespan, PrintCurrent(), reuse, p); } diff --git a/test/DryIoc.IssuesTests/GHIssue516_Singleton_Decorator_to_Scoped_base_should_not_work_but_does.cs b/test/DryIoc.IssuesTests/GHIssue516_Singleton_Decorator_to_Scoped_base_should_not_work_but_does.cs new file mode 100644 index 000000000..71cf48fb6 --- /dev/null +++ b/test/DryIoc.IssuesTests/GHIssue516_Singleton_Decorator_to_Scoped_base_should_not_work_but_does.cs @@ -0,0 +1,41 @@ +using NUnit.Framework; + +namespace DryIoc.IssuesTests +{ + [TestFixture] + public class GHIssue516_Singleton_Decorator_to_Scoped_base_should_not_work_but_does : ITest + { + public int Run() + { + Example(); + return 1; + } + + [Test] + public void Example() + { + var c = new Container(); + c.RegisterMany(Reuse.Scoped); + c.Register(Reuse.Singleton, setup: Setup.Decorator); + + using (var s1 = c.OpenScope()) + { + var ex = Assert.Throws(() => s1.Resolve()); + Assert.AreEqual(Error.NameOf(Error.DependencyHasShorterReuseLifespan), ex.ErrorName); + } + } + + public interface IFace { } + + public class A : IFace { } + + public class ADecorator : IFace + { + public IFace Inner { get; } + public ADecorator(IFace inner) + { + Inner = inner; + } + } + } +} \ No newline at end of file