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.