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

Sometimes resolve operation throws DependencyResolutionException if the scope is disposed from another thread #945

Closed
Alexandr-Gurinovich opened this issue Nov 5, 2018 · 0 comments

Comments

@Alexandr-Gurinovich
Copy link

I have registered instance A of interface IA.
Sometimes when I resolve an interface IA and dispose a scope from concurrent thread I get DependencyResolutionException while ObjectDisposedException is expected.

Sample test to reproduce:

        private interface IA
        {
        }

        private class A : IA
        {
        }

        [Fact]
        public async Task RepeatedResolveExternallyOwnedInstanceWhileTheScopeIsDisposing_ObjectDisposedExceptionThrownOnly()
        {
            for (int i = 0; i < 200; i++)
            {
                await ResolveExternallyOwnedInstanceWhileTheScopeIsDisposing_ObjectDisposedExceptionThrownOnly();
            }
        }

        private async Task ResolveExternallyOwnedInstanceWhileTheScopeIsDisposing_ObjectDisposedExceptionThrownOnly()
        {
            var cb = new ContainerBuilder();
            var container = cb.Build();

            var scope = container.BeginLifetimeScope(
                "name", builder =>
                {
                    var a = new A();
                    builder.RegisterInstance<IA>(a);
                });

            var t = Task.Run(
                () =>
                {
                    try
                    {
                        while (true)
                        {
                            scope.Resolve<IA>();
                        }
                    }
                    catch (ObjectDisposedException e)
                    {
                    }
                });
            await Task.Delay(5);

            scope.Dispose();

            await t;
        }
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

1 participant