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

ReturnDefaultIfNotRegistered is not respected between scopes #434

Closed
rick-palmsens opened this issue Oct 21, 2021 · 2 comments
Closed

ReturnDefaultIfNotRegistered is not respected between scopes #434

rick-palmsens opened this issue Oct 21, 2021 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@rick-palmsens
Copy link

rick-palmsens commented Oct 21, 2021

The following code will throw even though IfUnresolved is set to ReturnDefaultIfNotRegistered:

class Program
{
    static void Main(string[] args)
    {
        var container = new Container();

        using (var scope = container.OpenScope())
        {
            var foo = scope.Resolve<Foo>(IfUnresolved.ReturnDefaultIfNotRegistered); // Does not throw
            scope.UseInstance<Foo>(new Foo()); // Do a registration within the scope
        }

        using (var scope = container.OpenScope())
        {
            var foo = scope.Resolve<Foo>(IfUnresolved.ReturnDefaultIfNotRegistered); // Throws
        }
    }
}

Since the registration happened within the first scope, I'd expect the second scope to simple be empty and not throw. Looking at the stack trace, the IfUnresolved parameter seems to get discarded half way through, and the code simply throws instead of checking if it should actually throw.

@dadhi
Copy link
Owner

dadhi commented Oct 21, 2021

@rick-palmsens Thanks for finding, will check. The fix PRs are appreciated as well (as you have already looking into the stack trace) ;)

@dadhi dadhi self-assigned this Oct 30, 2021
@dadhi dadhi added the bug Something isn't working label Oct 30, 2021
@dadhi dadhi added this to the v4.8.4 milestone Oct 30, 2021
@dadhi
Copy link
Owner

dadhi commented Oct 30, 2021

I have fixed the issue in the upcoming v4.8.4 but going forward please use Use instead of UseInstance as the later will be removed in the next major version.

@dadhi dadhi closed this as completed in e94a67f Oct 30, 2021
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