1

My singleton object does not keep its state when loading a new page in blazor.

I have this interface:

public interface IPreLaunchSession
    {
        bool IsPreLaunchAuthenticated { get; set; }
    }

The class for this interface

 public class PreLaunchSession : IPreLaunchSession
    {

        public bool IsPreLaunchAuthenticated { get; set; }

        public PreLaunchSession()
        {

        }
    }

I register it as a singleton

builder.Services.AddSingleton<IPreLaunchSession,PreLaunchSession>(); 

Then inject it into the page

@inject Services.IPreLaunchSession PreLaunchSession

In these pages where its injected, I test for the bool variable as well as set it.

In another section of code, I redirect using NavigationManager. Something like this:

Navigation.NavigateTo("/MyOtherPath");

This seems to work for a while but then a <NavLink/> is used to go to another page and as soon as this happens then the PreLaunchSession object seems to be recreated and the bool variable in it is reset to default (which is false)

Any help here would be great in understanding why a singleton object gets recreated with navigation happening.

GregoryBrad
  • 1,145
  • 13
  • 18
  • What flavour of Blazor do you use ? Please, post the *complete* code of your service, and some code sample from a component using this service to demonstrate how you employ it. – enet Oct 06 '20 at 14:20
  • *the PreLaunchSession object seems to be recreated* - Do you know this for a fact, as in did you set a breakpoint in the constructor to verify it hits twice? – insane_developer Oct 06 '20 at 15:00
  • It seems to happen when the DLL is loaded again. Which does make sense, its just a question of how do I navigate without reloading the DLL again. @HenkHolterman I think you are on to something, it might be to do with the way navigation works in blazor. – GregoryBrad Oct 07 '20 at 06:25
  • Turns out this is not an issue or a bug it is by design. When I F5 refresh the app it's like shutting the app down and starting it up again. This would of course reset the state of all singleton objects. Here is a thread on it. https://stackoverflow.com/questions/63347056/how-to-persist-value-in-a-singleton-state-container-in-blazor-web-assembly-on-pa – GregoryBrad Oct 07 '20 at 08:45

1 Answers1

1

Have a look at how to persist value in a singleton state container in blazor web assembly on page reload

When refreshing the app (F5 etc) the app is essentially turned off and turned on again.

This would cause the whole app to restart and all memory of persisted singleton objects would be lost.

I ended up using localstorage to persist between full refreshes or tab changes.

GregoryBrad
  • 1,145
  • 13
  • 18
  • I've got a similar problem - exactly the same scenario, I've got a singleton service but when I navigate, the page I navigate to can't access values of members I've set in the singleton service from the previous page. I've got other data in local storage, but I was hoping not use that for persisting between sessions, not pages. – Matt G Nov 26 '20 at 12:54
  • 1
    If the app reloads, then the values in memory are reset to the default. There is no "Session" like in asp.net. Remember you are now working on the client-side. – GregoryBrad Nov 30 '20 at 07:02
  • Yep true. My problem is if I navigate from /pageA to /pageB using the injected navigation service, everything works. But if I am on /pageA and I type /pageB in the URL, I get a new session. – Matt G Dec 01 '20 at 01:21
  • That's by design, you are in essence reloading the app. – GregoryBrad Dec 11 '20 at 07:43
  • That's a shame - in an Angular app the router captures URL changes from the address bar – Matt G Dec 15 '20 at 07:20
  • 1
    @MattG I found that for a Singleton class, as long as you inject the singleton on each page, it holds its state or same session. The only time it does not is when F5 is hit, this is true for any language. Angular does some stuff behind the scenes for you, which is nice, but removes full control. – Lawrence Thurman Jul 18 '21 at 13:23