Lukaa
Lukaa
CC#
Created by Lukaa on 4/30/2024 in #help
Proper way to do per user state management in Blazor with Identity
Hi. I am attempting to have state management with blazor & identity. So basically I want to acquire the current user in the ManageLayout and then reuse the data in navmenu or in @body. I was thinking of just passing them as parameters, but you need some kind of synchronization due to asynchronous user retrieval. this is my state code :
internal sealed class AccountDataState
{
public string? Username { get; private set; }
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IdentityUserAccessor _userAccessor;

public AccountDataState(IHttpContextAccessor httpContextAccessor, UserManager<ApplicationUser> userManager, IdentityUserAccessor userAccessor)
{
_httpContextAccessor = httpContextAccessor;
_userManager = userManager;
_userAccessor = userAccessor;
}

public event Action OnChange;
public async Task InitializeAsync()
{
var user = await _userAccessor.GetRequiredUserAsync(_httpContextAccessor.HttpContext);
Username = await _userManager.GetUserNameAsync(user);
NotifyStateChanged();
}
private void NotifyStateChanged() => OnChange?.Invoke();
}
internal sealed class AccountDataState
{
public string? Username { get; private set; }
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IdentityUserAccessor _userAccessor;

public AccountDataState(IHttpContextAccessor httpContextAccessor, UserManager<ApplicationUser> userManager, IdentityUserAccessor userAccessor)
{
_httpContextAccessor = httpContextAccessor;
_userManager = userManager;
_userAccessor = userAccessor;
}

public event Action OnChange;
public async Task InitializeAsync()
{
var user = await _userAccessor.GetRequiredUserAsync(_httpContextAccessor.HttpContext);
Username = await _userManager.GetUserNameAsync(user);
NotifyStateChanged();
}
private void NotifyStateChanged() => OnChange?.Invoke();
}
Currently this doesn't work in another component, when Username is initialized it doesn't really notify all components even though the state is injected, I assume that's due to service being Scoped. How is the conventional way to approach this issue? ManageLayout.razor :
protected override async void OnInitialized()
{
State.OnChange += StateHasChanged;
await State.InitializeAsync();
}
protected override async void OnInitialized()
{
State.OnChange += StateHasChanged;
await State.InitializeAsync();
}
Index.razor
<h1>@username</h1>
@code{
protected override async Task OnInitializedAsync()
{
username = State.Username;
}
<h1>@username</h1>
@code{
protected override async Task OnInitializedAsync()
{
username = State.Username;
}
2 replies
CC#
Created by Lukaa on 4/27/2024 in #help
Docker + Blazor + MSSQL handshake error
When I run dotnet ef database update I get the following error : Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 35 - An internal exception was caught) I'm using WSL btw
12 replies
CC#
Created by Lukaa on 1/31/2024 in #help
WCF TransportBindingElement error with default bindings - basicHttpBinding
Hi, I have a WCF service that I self host on a console app, and when trying to consume it on the client I get an error saying that every custom binding requires a transport binding element, but I'm literally running on default configs. Anyone had a similar experience?
2 replies
CC#
Created by Lukaa on 4/21/2023 in #help
✅ Get X and Y of selected text using WINAPI
Hi, I'd like to display custom small WPF window on text selection by getting X and Y coords via WinAPI, similarly to how context menus work in windows. Ive seen tons of apps do that so it should be possible.
34 replies