im using blazor server side and are therefore trying to use AddScoped instead of AddSingleton as the object is used on a per-user basis. I try to split the razor pages and the c# code as much as posible as i find this to be cleaner.
I add a scoped service using
Services.AddScoped<Services.ManageUserService>();
in the ConfigureServices function of the Startup class.
my problem now is to properly access the service from any .cs file (holding the logic of my .razor pages)
I have tried to do an injection like this:
[Inject]
public Services.ManageUserService manageUserService { get; set; }
and then accesed the scoped object using (username for example):
manageUserService.User
and this works. My problem is that if I add a print that is supose to only run once within the scoped service it runs every time the page is reloaded or changed.
for example, lets say i do this:
public class ManageUserService
{
public string User { get; private set; }
private bool LoadedStartup = false;
public ManageUserService() => SetupUser();
private void SetupUser()
{
if (!LoadedStartup)
{
User = "me";
LoadedStartup = true;
System.Diagnostics.Debug.Print("User:" + User);
}
}
}
I then access the class from multiply .cs files using:
[Inject]
public Services.ManageUserService manageUserService { get; set; }
The print "User:me" is supposed to only happen once as the locking bool LoadedStartup is changed, problem is that I get the print every time the Inject is happening (on change page, etc)
What am I doing wrong? aren't the AddScoped() suppose to add a "singelton" instance for every client? am I accessing it wrongly?
I can't find any examples of using AddScoped from separated .cs and .razor pages, only directly from the .razor page, and then it is done using @inject.