Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transient resolve with opening scope using factory func in singleton #507

Closed
BinaryCraX opened this issue Jul 28, 2022 · 3 comments
Closed
Assignees
Milestone

Comments

@BinaryCraX
Copy link

When a transient service (which has openResolutionScope set) gets resolved using a factory-func from inside another service which has Reuse.Singleton set, then this resolve won't open a new scope as I would expected.

Is this behavior the desired behavior and I am just using it wrong?

[TestFixture]
class Transient_resolve_with_scope_using_factory_func_in_singleton
{
    [Test]
    public void Example()
    {
        var container = new Container(rules => rules
            .WithTrackingDisposableTransients()
        );

        container.Register<Bar>(Reuse.Singleton); // if "Reuse.Singleton" gets removed, the scope ist opened (and injected as part of IResolverContext) as expected.
        container.Register<IFoo, Foo>(setup: Setup.With(openResolutionScope: true));

        container.Register<Dependency>(Reuse.ScopedTo<IFoo>());

        var bar = container.Resolve<Bar>();
        bar.DoStuffWithFoo();
    }

    class Bar
    {
        private readonly Func<IFoo> fooFactory;
        private IFoo foo;

        public void DoStuffWithFoo()
        {
            foo = fooFactory(); // expecting this to open a scope.
            foo.Dispose();
        }

        public Bar(Func<IFoo> fooFactory)
        {
            this.fooFactory = fooFactory;
        }
    }

    interface IFoo : IDisposable
    {
        Dependency Dep { get; }
    }

    class Foo : IFoo
    {
        public Dependency Dep { get; }

        private readonly IResolverContext _scope;
        public Foo(Dependency dep, IResolverContext scope)
        {
            Dep = dep;
            _scope = scope;

            Assert.IsTrue(scope.CurrentScope != null, "Expected to have a scope.");
        }

        public void Dispose() => _scope.Dispose();
    }

    class Dependency : IDisposable
    {
        public bool IsDisposed { get; private set; }
        public void Dispose() => IsDisposed = true;
    }
}

Used DryIoc-Version: 5.1.0

@dadhi
Copy link
Owner

dadhi commented Jul 28, 2022

@BinaryCraX Interesting, will look.

@dadhi dadhi self-assigned this Jul 28, 2022
dadhi added a commit that referenced this issue Aug 1, 2022
@dadhi dadhi added this to the v5.2.0 milestone Aug 1, 2022
@dadhi dadhi closed this as completed in 5b653f6 Aug 2, 2022
@dadhi
Copy link
Owner

dadhi commented Aug 3, 2022

@BinaryCraX v5.2.0 with the fix is out.

@BinaryCraX
Copy link
Author

Thank you very much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants