Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

CustomStateParameters available in different components, .NET 8

$
0
0

I have a class with some currentstate parameters (instead of session variables)

In program.cs:

builder.Services.AddScoped<CurrentState>();    

Can I inject this CurrentState and read / update parameters at runtime?

When I tried this every component that inject this with @inject CurrentState currentState, every currentState = null and get a new hashcode (currentState.GetHashCode())

I've read an article somewhere that you can do this but can't find it again. Is it totally wrong and I must use sessions or is there a way to use this ang get same instance of CurrentState in all components?

Found an article similiar : https://wellsb.com/csharp/aspnet/blazor-singleton-pass-data-between-pages#google_vignette

I use serverside so can't use .Singleton. But it seams like I get a completely new instance on every injection?

Maybe I can use cascadingparameters in Routes.razor?

CustomStateSevice

class CustomStateService {  public User UserItem {get;set}  // There is a class with Userdata  public int SomeIntData {get;set;}

Program.cs

builder.Services.AddScoped<CustomStateService>();    builder.Services.AddScoped<UserInitializationService>();    builder.Services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>    {        options.Events = new OpenIdConnectEvents        {            OnTokenValidated = async ctx =>            {                var userState = ctx.HttpContext.RequestServices.GetRequiredService<CustomStateService>();                var userInitializationService = ctx.HttpContext.RequestServices.GetRequiredService<UserInitializationService>();                var user = ctx.Principal;                await userInitializationService.InitializeUserAsync(user);            }        };    });

UserInitializationService

public class UserInitializationService{    private readonly CustomStateService _customState;    private readonly IUserRepository _userRepository;    public UserInitializationService(CustomStateService customStateService, IUserRepository userRepository)    {        _customState = customStateService;        _userRepository = userRepository;    }    public async Task InitializeUserAsync(ClaimsPrincipal user)    {        if (user.Identity.IsAuthenticated)        {            var tempUser = new User            {                AdId = user.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value,                Email = user.Identity.Name,                Name = user.FindFirst("name").Value,                LatestActivty = DateTime.Now            };            _customState.UserItem = await _userRepository.CheckUser(tempUser);        }        else        {            _customState.UserItem = null;        }    }}

And in component

@inject CustomStateService _csService@code{...    User userItem = _csService.UserItem;...

Now every parameter in userItem is null.

Maybe this only works in webassembly where you can use .AddSingleton(); ?

Maybe it's better to set my CustomStateService value in Routes.cs and then distribute this as a CascadingParameter?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>