everyone!!
I am a student who has just started learning Blazor (.NET 8/10), and I am completely confused about how to correctly transfer session data from SSR (server-side rendering) to Interactive Server mode.
My task is simple: I have my own session data (such as SessionId, tokens, UserId) that is loaded into middleware and stored in HttpContext.Items (all of this is packed into a class for simplicity, of course). I need this data to be available in interactive components. Microsoft says it's better not to use HttpContext in interactive parts, but it's not clear how to do this correctly.
What I tried:
SessionProvider wrapper: I made a component with InteractiveServer and ChildContent to wrap everything.
Result: Serialization error, RenderFragment is not passed.
Initialization in Middleware: I tried to fill the ISessionKeyManager service in middleware.
Result: A new scope is created in Interactive, and the data is lost.
Interactive Layout: I put InteractiveServer in MainLayout.
Result: Same error with Body serialization.
PersistentState on pages: Added [PersistentState] directly to pages such as Settings.
Result: Works, but the code is repeated everywhere. So far, the coolest solution, I tried to poke around a bit, but still had to copy and paste from Microsoft docs, for some reason it didn't work any other way, but that's not the point. The main problem here is that the code has to be copied to every page, and I would like to have a common entry point.
CircuitHandler: I read articles where it is used for transfer from HttpContext.
Result: Haven't tried it yet, but it looks a bit crude, doesn't it?
So many ways, but which one is correct? I read up on it and understood that PersistentComponentState is the official one from Microsoft, but I'd still like to hear some outside opinions.
What approach does Microsoft recommend for a custom session with AuthStateProvider?
When should I use PersistentComponentState, and when should I use CircuitHandler?
Is it correct that PersistentComponentState is the main one, and if not, then what is?
How should I organize the code so that I don't have to copy the initialization to every page?
Is it worth struggling with SSR, or is it easier to just turn it off and be done with it?
The topic is too general, I'm not sure if code examples and such are needed here, the question is more theoretical after all.
Please help with advice or links! 🙏