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

Update transient services guidance #31705

Merged
merged 4 commits into from
Feb 6, 2024

Conversation

guardrex
Copy link
Collaborator

@guardrex guardrex commented Feb 6, 2024

Fixes #31689
Addresses #28161

Blazor samples were updated on dotnet/blazor-samples#181.


Internal previews

📄 File 🔗 Preview link
aspnetcore/blazor/fundamentals/dependency-injection.md ASP.NET Core Blazor dependency injection

@guardrex guardrex self-assigned this Feb 6, 2024
Copy link
Member

@MackinnonBuck MackinnonBuck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a question about the guidance for using scoped services with server interactivity.

aspnetcore/blazor/fundamentals/dependency-injection.md Outdated Show resolved Hide resolved
@guardrex guardrex merged commit ff38463 into main Feb 6, 2024
3 checks passed
@guardrex guardrex deleted the guardrex/blazor-transient-disposables branch February 6, 2024 19:42
@hakenr
Copy link
Member

hakenr commented Feb 7, 2024

@guardrex @MackinnonBuck I'd recommend adding one or two sentences explaining why transient disposables are a problem and why it's important to avoid them.

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 7, 2024

Already done ... it's in the first (earlier) section on OwningComponentBase. These "detector" sections are just detection for those who feel that devs might add transient services in the future for some reason or perhaps have a lib add them inadvertently (although I didn't explicitly call libs out).

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0#utility-base-component-classes-to-manage-a-di-scope

@hakenr
Copy link
Member

hakenr commented Feb 7, 2024

@guardrex Not sure if you're referring to this section?

In ASP.NET Core apps, scoped services are typically tied to the current request. Once the request is completed, any scoped or transient services are disposed of by the DI system. On the server side, the request scope lasts for the duration of the client connection, potentially causing transient and scoped services to live much longer than anticipated. On the client side, services registered with a scoped lifetime behave as singletons, meaning they persist longer than scoped services in typical ASP.NET Core applications.

I've discussed this with several colleagues, and despite their current knowledge and after going through this section of the documentation, they're unsure why transient disposables are particularly problematic, mainly because they haven't been following the Blazor issue tracking and haven't had the chance to look into any related issues:

(Just an observation from the field, you might have different view.)

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 7, 2024

I'll raise that with Mackinnon on the new PR.

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 7, 2024

Ok ... it's live now ...

These updates went in yesterday. I guess I agreed with you! 😆 ... because I did make quite an update to the lead-in bits.

In spite of this PR being closed, take a look at it now here and let me know if you still have concerns about it. We'll avoid opening yet another issue in a SEA of Blazor issues at the moment 😩. I'm working fast to get the backlog down because we're starting to document .NET 9 NOW 😅.

https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/dependency-injection?view=aspnetcore-8.0#utility-base-component-classes-to-manage-a-di-scope

@hakenr
Copy link
Member

hakenr commented Feb 7, 2024

It's much clearer now, but it still doesn't explain why specifically transient disposables are problematic. (The introduction mentions transient services in general, which might leave readers questioning why disposable services, in particular, should be identified as an issue.)

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 7, 2024

I think the "disposables" part could be dropped. I'll open an issue to discuss this subject further with Mackinnon.

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

Successfully merging this pull request may close these issues.

Transient disposable section updates
3 participants