The Wayback Machine - https://web.archive.org/web/20211202072034/https://github.com/dotnet/aspnetcore/issues/38764
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

[WebAssembly hosted in .net core] Make scoped service session dependant #38764

Open
julienGrd opened this issue Dec 1, 2021 · 3 comments
Open

[WebAssembly hosted in .net core] Make scoped service session dependant #38764

julienGrd opened this issue Dec 1, 2021 · 3 comments
Assignees
Milestone

Comments

@julienGrd
Copy link

@julienGrd julienGrd commented Dec 1, 2021

Hello guys, i have a blazor app which run in both mode : server and webassembly
We mostly tested it in server mode and it works perfectly
We now try to test it in webassembly and we face a big problem : all the scoped service injected on the server are recreated at each request, scoped service which contain data i don't want to loose
It seem the default behavior rafter reading the docs, but i was thinking the scoped service will be session-dependant

The session on the webassembly mode is manage by a cookie

This is a big difference with the server side where my scoped service on the server are created one time for a client.

So now i don't know how to deal with that, should i use totally a different system between the server mode and the webassembly mode ? (which was not the objective, i try less as possible to make specific code for one or the other).

because if i begin to work with session object rather than scoped service, it will be certainly the server side app which we will stop working good because there is no session.

what will be your general guidelines to solve this problem with maintain session state on the server for both the webassembly and the server mode ?

I wait for your suggestions

Thanks

@pranavkm
Copy link
Contributor

@pranavkm pranavkm commented Dec 1, 2021

@julienGrd you should be able to use a runtime check to determine if you're executing in a Blazor WASM app and use that to conditionally register a service as a singleton:

if (OperatingSystem.IsBrowser()
   services.AddSingleton<IService, MyService>();
else
   services.AddScoped<IService, MyService>();

Admittedly it's not a great way to go about it, but this is also the first we've seen any feedback on this, so we'd like to wait to see if we'd like to tackle this more broadly.

Loading

@julienGrd
Copy link
Author

@julienGrd julienGrd commented Dec 1, 2021

@pranavkm thanks for your help. Actually the problèm is focus on the server part (even in webassembly mode). There is nothing related to the client. And its not really related to blazor, more to asp .net core.
If i Can resume what i need, there is a way to make a scoped service on the server with a lifetime depending of the session of the user ? Actually thé scope is per request when i run in webassembly mode, but works perfectly (by connection so by "session") when it run over signalr.
Sorry if im not clear

Loading

@pranavkm
Copy link
Contributor

@pranavkm pranavkm commented Dec 1, 2021

Sorry if I'm not following along, but is the ask to have a service that is tied to a user's session? For a Blazor Server app, you should get this by default, and infact slightly better than vanilla SignalR since a scope is tied to a circuit which can exceed a single SignalR connection.

For a Blazor WASM app, a singleton service would be the closest approximation - a user is in session as long at they have the Blazor WASM app running. If you meant to have it actually tied to a browser session and survive app restarts / browser refreshes, unfortunately there is nothing in the framework that solves this. The closest would be to stash data in the browser's sessionStorage / localStorage and use that to recreate your services as part of startup.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants