If i have a service that stores a token as a lazy object, and the token is fetched inside a value factory, is that value persisted across multiple requests, or since the underlying service is scoped, lazy starts with a null value for every single request?
_token = new Lazy<Token>(() =>
{
var token = new Token();
var task = Task.Run(async () =>
{
var response = await Login();
token.AccessToken = response.AccessToken;
token.RefreshToken = response.RefreshToken;
});
task.Wait();
return token;
});
This is called in the constructor of the service:
public MyService()
{
if (_token is { IsValueCreated: false }) // execute logic above
}
However, MyService is registered as scoped in Startup.cs,
services.AddScoped<IMyService, MyService>();
Does this mean that the token is fetched every time service is requested (despite the fact that value factory is thread safe) or the value factory can be called only once (even if it is called from within multiple scopes)?