I know this doesn't really help you now. But the Blazor team have seen their shortfalls on specifically this. They are currently adding in [SupplyParameterFromPersistentComponentState] (name to be refactored when they decide on a more catchy phrase), on components and in the middle of adding something for services, see here: https://github.com/dotnet/aspnetcore/pull/60634
You may be able to achieve some janky work-around with PersistentComponentState (see here for example https://learn.microsoft.com/en-us/aspnet/core/blazor/components/prerender?view=aspnetcore-9.0)
I've done this by creating a new Layout Base Class.
i.e.
public class BaseLayoutComponent : LayoutComponentBase{
[Inject] public PersistentComponentState ApplicationState
}
Then in your MainLayout inherit the custom base class instead of the standard and consume it that way
@inherits BaseLayoutComponent
Hope this helps you a bit.
If you think of anything else let me know because I'm also trying janky solutions until .Net10 rolls out with the fix.
Here's a video that might also help
https://youtu.be/3Yc7Bbx1mFM
Basically every storage solution for a blazor projects means running js interop (session, local, cookie, indexdb) but obviously when we're doing things correctly and getting data within OnInizializedAsync methods if we want to stop any UI janky behaviour and you want to persist any data it has to be within OnAfterRenderAsync so that the runtime is available causing all kinds of crap and going against the grain of Blazor razor component life-cycles.