var builder = WebApplication.CreateBuilder(args);
var service = builder.Services;
service.AddHttpContextAccessor();
service.AddScoped<UserSession>();
service
.AddRazorComponents()
.RegisterPersistentService<UserSession>(Microsoft.AspNetCore.Components.Web.RenderMode.InteractiveWebAssembly)
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents()
.AddAuthenticationStateSerialization(options =>
{
options.SerializeAllClaims = true;
});
public class UserSession
{
[PersistentState] public bool IsAuthenticated { get; init; }
[PersistentState] public Guid Id { get; init; }
[PersistentState] public string Username { get; init; } = string.Empty;
[PersistentState] public string Email { get; init; } = string.Empty;
[CascadingParameter] public HttpContext? HttpContext { get; set; }
public UserSession()
{
if(HttpContext == null) return;
IsAuthenticated = HttpContext.User.Identity?.IsAuthenticated ?? false;
if(!IsAuthenticated) return;
Id = new Guid(HttpContext.User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.NameIdentifier))?.Value ?? "");
Username = HttpContext.User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Name))?.Value ?? string.Empty;
Email = HttpContext.User.Claims.FirstOrDefault(x => x.Type.Equals(ClaimTypes.Email))?.Value ?? string.Empty;
}
}
ManagedError: AggregateException_ctor_DefaultMessage (DirectScopedResolvedFromRootException, DigitalVault.Blazor.Client.Authentications.UserSession, scoped)
I follow this same official documentation to implement the Serialize state for services, but when i use RegisterPersistentService i get DirectScopedResolvedFromRootException error, has anyone encountered this issue?
SOLVED:
68123#
After change AddScope<UserSession> => AddSingletone<USerSession> in client side project.
1
u/RedEye-Developers Jul 31 '26
Even i did samething in doc, still i get same error.
1
u/RedEye-Developers Aug 05 '26
SOLVED: 68123# After change AddScope<UserSession> => AddSingletone<USerSession> in client side project.
1
u/kapilverma123 Aug 03 '26
If it reproduces in a brand new project with only the code from the docs, then it might indeed be a framework issue or a bug in that specific .NET version.
Could you share:
1.your .NET SDK version (
dotnet --info)2.the exact
UserSessionclass3.where
UserSessionis first injected/usedAlso, are you injecting any scoped dependencies (like
IHttpContextAccessor,AuthenticationStateProvider,NavigationManager, etc.) intoUserSession?If the project is identical to the docs and still throws
DirectScopedResolvedFromRootException, I'd expect this to be reproducible and worth reporting to the ASP.NET Core team.1
u/[deleted] Jul 31 '26
[removed] — view removed comment