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

Improve error reporting when resolving Scope instances when there is no active scope #702

Closed
dotnetjunkie opened this issue May 1, 2019 · 2 comments

Comments

@dotnetjunkie
Copy link
Collaborator

dotnetjunkie commented May 1, 2019

The private Container.AddContainerRegistrations methods registers the Scope type using the specially crafted ScopedScopeLifestyle, but there are some quirks with that approach:

  • The ScopedScopeLifestyle.GetCurrentScopeCore method never returns null, but instead throws an exception. This complicates the code, because in AddContainerRegistrations it seems as if the registered delegate can actually return null (this actually came up when working on Implement C# 8 non-nullable support #649).
  • When there is no active scope, ScopedScopeLifestyle throws a simple "There is no active scope" message, which is too minimalistic and, therefore, confusing to the user as it does not explain what scope we are talking about and how to fix the problem.
  • ScopedScopeLifestyle.GetCurrentScopeCore calls container.GetVerificationOrResolveScopeForCurrentThread, but that will always return null, since ScopedLifestyle.GetCurrentScope always returns that value before calling into GetCurrentScopeCore. This statement was incorrect as a created "current scope provider" (created by calling ScopedLifestyle.CreateCurrentScopeProvider) bypasses GetCurrentScope and directly calls the private GetScopeFromDefaultScopedLifestyle. Removing this check, therefore, causes an incorrect exception during verification.

It might be better to completely remove ScopedScopeLifestyle and the indirection it causes, but in that case a different way must be found to allow registering Scope with a scoped lifestyle, as this method is called early in the pipeline (Container ctor), at a stage that DefaultScopedLifestyle is not set.

@dotnetjunkie
Copy link
Collaborator Author

feature-702 branch created.

@dotnetjunkie
Copy link
Collaborator Author

After more thorough investigation, the ScopedScopeLifestyle allows errors to be reported in a way that would otherwise be hard to achieve. For instance by throwing the right messages during verification. Because of that, only the thrown message texts will be improved in this fix.

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

No branches or pull requests

1 participant