-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Design for solving transient disposables on Blazor Server #26676
Comments
Thanks for contacting us. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Can I assume backlog is another way to say, we're going to do it for 6.0 😬 |
Have there been more discussions about this since? Could someone share the current thoughts so we can talk through it? |
Design Proposal: Transient disposables on Blazor ServerSummaryAllow for more granular control over the lifetime of transient services implementing MotivationTransient services which implement
The DI container for Blazor Server cannot be garbage collected until the circuit is terminated. Consequently, transient services implementing Given the lifetime of the circuit may be much longer than the lifetime of the component, we're unduly delaying the disposal of transient services implementing In-scope
We want to ensure that transient services implementing Out of scopeDepending on the solution chosen, it may be possible for users to purposely not dispose of the transient service upon component disposal. This would be discouraged, but it may not be possible to entirely prevent. Proposed SolutionsImplicitly Manage Service LifetimeHave the Blazor framework handle the lifetime of The Blazor framework requests the DI container (Service Provider) for a tracker. A tracker is maintained for each component created. All transient services created for a specific component are associated with the tracker. When the component is disposed, the tracker is disposed, thereby disposing the Framework code: IServiceProvider provider;
// Create a tracker for the component.
var tracker = provider.CreateTracker(); // [NEW]
// `IDisposable` transient services injected into the component are created
// using the tracker.
tracker.GetRequireService<ITransientDisposableService>();
// The tracker is disposed when the component is disposed.
// The `IDisposable` transient services created using the tracker are disposed via the tracker disposal.
await tracker.DisposeAsync(); Advantages
Disadvantages
Explicitly Manage Service LifetimeAllow users to manage the lifetime of Users wrap the User code: @inject ITracked<ITransientDisposableService> trackedTransientDisposableService
@{
var service = trackedTransientDisposableService.Value;
trackedTransientDisposableService.Dispose();
} Framework code: IServiceProvider provider;
return provider.GetRequiredService<ITracked<ITransientDisposableService>>(); Definition interface ITracked<T> : IDisposable
{
T Value { get; }
} Advantages / Disadvantages are essentially the inverse of the explicit approach. Advantages
Disadvantages
Hybrid ApproachAllow both implicit and explicit management. Implicitly manage Advantages
Disadvantages
Leverage
|
/// <summary> | |
/// Gets the scoped <see cref="IServiceProvider"/> that is associated with this component. | |
/// </summary> | |
protected IServiceProvider ScopedServices |
User code:
@implements OwningComponentBase
@{
var service = ScopedServices.GetService<ITransientDisposableService>();
}
The disposal of the component results in the disposal of the OwningComponentBase
. Thus, the ScopedServices
provider is garbage collected, and the transient services implementing IDisposable
are disposed.
Advantages
- Already implemented & available for use. No changes required to the Dependency Injection Container.
- No potentially breaking changes.
Disadvantages
- Learning curve to understand how to implement the pattern.
- Requires users to work manually with the Dependency Injection Container (can't use Blazor's
@inject
). - Doesn't automatically resolve the issue for existing apps not using the pattern.
Risks / Unknowns
Varies based on the approach chosen. The disadvantages sections cover the risks and unknowns of the approach.
Requires updating the Dependency Injection Container implementation which is used broadly. Different stakeholders may have differing concerns and requirements.
Examples
See above.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Thanks for contacting us. We're moving this issue to the |
This is something we've discussed in the past, and previously decided that we needed a notion of nested DI scopes to solve truly. If this is possibly in scope for .NET 6, we need to begin an actual design process.
cc @javiercn @davidfowl
The text was updated successfully, but these errors were encountered: