From 5fe8288b1b85febbb85cbf38702ece86e980d9f9 Mon Sep 17 00:00:00 2001 From: dadhi Date: Sun, 31 Oct 2021 13:41:23 +0100 Subject: [PATCH] fixed: #435 --- src/DryIoc/Container.cs | 10 ++++++---- .../Issue530_Multi_tenancy_support.cs | 9 ++++++--- ...hangfire_use_dryioc_report_ContainerIsDisposed.cs | 12 ++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/DryIoc/Container.cs b/src/DryIoc/Container.cs index 1f2c247da..12242b354 100644 --- a/src/DryIoc/Container.cs +++ b/src/DryIoc/Container.cs @@ -4893,10 +4893,11 @@ public static class ResolverContext internal static readonly MethodInfo OpenScopeMethod = typeof(ResolverContext).GetTypeInfo().GetDeclaredMethod(nameof(OpenScope)); - /// Returns root or self resolver based on request. + /// Returns root resolver for the singletons or the non scoped dependency of singletons, + /// or the current resolver for the rest. public static Expression GetRootOrSelfExpr(Request request) => - request.Reuse is CurrentScopeReuse == false && - request.DirectParent.IsSingletonOrDependencyOfSingleton && + request.Reuse is CurrentScopeReuse == false && + request.DirectParent.IsSingletonOrDependencyOfSingleton && !request.OpensResolutionScope && request.Rules.ThrowIfDependencyHasShorterReuseLifespan ? RootOrSelfExpr @@ -11796,7 +11797,8 @@ public override Expression CreateExpressionOrDefault(Request request) { // GetConstant here is needed to check the runtime state rule var delegateExpr = request.Container.GetConstantExpression(_factoryDelegate); - var resolverExpr = ResolverContext.GetRootOrSelfExpr(request); + // Here we are using the simplified GetRootOrSelfExpr - if we injecting resolver in the singleton it should be the root container + var resolverExpr = request.Reuse is SingletonReuse ? ResolverContext.RootOrSelfExpr : FactoryDelegateCompiler.ResolverContextParamExpr; return Convert(Invoke(delegateExpr, resolverExpr), request.GetActualServiceType()); } diff --git a/test/DryIoc.IssuesTests/Issue530_Multi_tenancy_support.cs b/test/DryIoc.IssuesTests/Issue530_Multi_tenancy_support.cs index 1633019f9..286fce812 100644 --- a/test/DryIoc.IssuesTests/Issue530_Multi_tenancy_support.cs +++ b/test/DryIoc.IssuesTests/Issue530_Multi_tenancy_support.cs @@ -51,14 +51,17 @@ public void No_cache_problem_between_Injected_singleton_and_scoped() c.Register(Reuse.Scoped); c.Register(); - Assert.IsInstanceOf(c.Resolve().Thing); + var user = c.Resolve(); + Assert.IsInstanceOf(user.Thing); using (var scope = c.OpenScope()) { - Assert.IsInstanceOf(scope.Resolve().Thing); + user = scope.Resolve(); + Assert.IsInstanceOf(user.Thing); } - Assert.IsInstanceOf(c.Resolve().Thing); + user = c.Resolve(); + Assert.IsInstanceOf(user.Thing); } public interface IThing {} diff --git a/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed.cs b/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed.cs index 696eb6b38..5708c6b16 100644 --- a/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed.cs +++ b/test/DryIoc.Microsoft.DependencyInjection.Specification.Tests/GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed.cs @@ -10,18 +10,19 @@ namespace DryIoc.Microsoft.DependencyInjection.Specification.Tests [TestFixture] public class GHIssue435_hangfire_use_dryioc_report_ContainerIsDisposed : DependencyInjectionSpecificationTests { - protected override IServiceProvider CreateServiceProvider(IServiceCollection services) => DryIocAdapter.Create(services); + protected override IServiceProvider CreateServiceProvider(IServiceCollection services) => + DryIocAdapter.Create(services); - [Test, Ignore("failing")] + [Test] public void SingletonFactory_Test() { var collection = new ServiceCollection(); - + collection.AddSingleton(r => new SingletonFactory(r)); collection.AddTransient(); - - + var serviceProvider = CreateServiceProvider(collection); + var scopeFactory = serviceProvider.GetService(); ISingletonFactory singletonFactory; using (var scope = scopeFactory.CreateScope()) @@ -39,7 +40,6 @@ public interface IFakeSingletonService { } public class FakeService : IFakeService, IFakeScopedService, IFakeSingletonService, IDisposable { public bool Disposed { get; private set; } - public void Dispose() { if (Disposed)