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

Allow an existing Scope to be provided to a ScopedLifestyle #880

Closed
dotnetjunkie opened this issue Jan 6, 2021 · 1 comment
Closed

Allow an existing Scope to be provided to a ScopedLifestyle #880

dotnetjunkie opened this issue Jan 6, 2021 · 1 comment

Comments

@dotnetjunkie
Copy link
Collaborator

ScopedLifestyle implementations contains an GetCurrentScope method that allows retrieving the scope for the current context. In some special circumstances, however, it could be beneficial to allow an existing scope to be provided and set as scope for the current context.

Microsoft's new Blazor uses a single IServiceScope for the duration of a single session. During callbacks (for instance when handling click events), however, the asynchronous context, which is used by the AsyncScopedLifestyle, gets reset, causing Simple Injector Scope to be gone. When a user calls Container.GetInstance, they might be prompted with an "the instance is requested outside the context of an active (Async) scope" exception.

To combat this, the user's infrastructure code that integrates Simple Injector with Blazor (or Simple Injector's own future integration package) could ensure the current Simple Injector Scope is set to the current asynchronous context using a ScopedLifestyle.SetCurrentScope method.

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