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

Func dependency on Singleton resolved under scope breaks after disposing scope when WithFuncAndLazyWithoutRegistration() #399

Closed
stevozilik opened this issue May 9, 2021 · 3 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@stevozilik
Copy link

Hi,

I'd like to consult behaviour of resolved Func dependency on a Singleton object under scope (standard Asp Net DryIoc lifecycle).

If the container is configured to support .WithFuncAndLazyWithoutRegistration(), the Func dependency of even a Singleton object constructed under scope stops working after the scope is disposed, even though no dependency has is marked as Reuse.Scoped.

class Program
{
    static void Main(string[] args)
    {
       using var container = new Container(rules => rules
        .WithFuncAndLazyWithoutRegistration());

        container.Register<DepFactory>(Reuse.Singleton);
        container.Register<Dep>(Reuse.Transient);

        DepFactory facoty = null;

        using (var scope = container.OpenScope())
        {
            facoty = scope.Resolve<DepFactory>();
        }

        var dep = facoty.Create();
    }

    class DepFactory
    {
        private readonly Func<Dep> depFunc;

        public DepFactory(Func<Dep> depFunc)
        {
            this.depFunc = depFunc;
        }

        public Dep Create()
        {
            // DryIoc.ContainerException: 'code: Error.ContainerIsDisposed;
            // message: Container is disposed and should not be used: "container with scope {IsDisposed=true, Name=null}
            // with Rules with { FuncAndLazyWithoutRegistration} has been DISPOSED!
            return depFunc.Invoke();
        }
    }

    class Dep
    { }
}

Note this is only a problem if the container rules include .WithFuncAndLazyWithoutRegistration(), Without that rule, the code works without exceptions.

Hence from my perspective the current behaviour is

  • Inconsistent - WithFuncAndLazyWithoutRegistration introducing different (more restrictive) dependency lifecycle behaviour
  • Undesirable - as despite being resolved under scope (e.g. Asp Net request), none of the dependencies has been registered as Scoped and so the failure is unexpected

I do appreciate that the behaviour is likely just a consequence of the current implementation (invoking disposed scoped resolver), but I think logically this is incorrect and it would make sense to at least offer an option for Func<> dependencies to ignore scope (provided the dependency is not registered as scoped)

@dadhi
Copy link
Owner

dadhi commented May 10, 2021

@stevozilik Thanks for the detailed issue description. Will look soon. PR with the fix is appreciated too.

@dadhi dadhi self-assigned this May 10, 2021
@dadhi dadhi added the bug Something isn't working label May 10, 2021
@dadhi dadhi added this to the v4.7.7 milestone May 10, 2021
@dadhi dadhi closed this as completed in 9c4db3a May 10, 2021
@dadhi
Copy link
Owner

dadhi commented May 10, 2021

@stevozilik The fix out with v4.7.7

@stevozilik
Copy link
Author

@dadhi excellent this works as expected now, thank you for such amazing turnaround

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants