Skip to content

Commit

Permalink
fixed: #435
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Oct 31, 2021
1 parent 65369d8 commit 5fe8288
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4893,10 +4893,11 @@ public static class ResolverContext
internal static readonly MethodInfo OpenScopeMethod =
typeof(ResolverContext).GetTypeInfo().GetDeclaredMethod(nameof(OpenScope));

/// <summary>Returns root or self resolver based on request.</summary>
/// <summary>Returns root resolver for the singletons or the non scoped dependency of singletons,
/// or the current resolver for the rest.</summary>
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
Expand Down Expand Up @@ -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());
}

Expand Down
9 changes: 6 additions & 3 deletions test/DryIoc.IssuesTests/Issue530_Multi_tenancy_support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ public void No_cache_problem_between_Injected_singleton_and_scoped()
c.Register<IThing, ScopedThing>(Reuse.Scoped);
c.Register<ThingUser>();

Assert.IsInstanceOf<SingleThing>(c.Resolve<ThingUser>().Thing);
var user = c.Resolve<ThingUser>();
Assert.IsInstanceOf<SingleThing>(user.Thing);

using (var scope = c.OpenScope())
{
Assert.IsInstanceOf<ScopedThing>(scope.Resolve<ThingUser>().Thing);
user = scope.Resolve<ThingUser>();
Assert.IsInstanceOf<ScopedThing>(user.Thing);
}

Assert.IsInstanceOf<SingleThing>(c.Resolve<ThingUser>().Thing);
user = c.Resolve<ThingUser>();
Assert.IsInstanceOf<SingleThing>(user.Thing);
}

public interface IThing {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISingletonFactory>(r => new SingletonFactory(r));
collection.AddTransient<IFakeService, FakeService>();



var serviceProvider = CreateServiceProvider(collection);

var scopeFactory = serviceProvider.GetService<IServiceScopeFactory>();
ISingletonFactory singletonFactory;
using (var scope = scopeFactory.CreateScope())
Expand All @@ -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)
Expand Down

0 comments on commit 5fe8288

Please sign in to comment.