Blazor root level Cascading Value doesn't propagate fresh value after change - Stack Overflow

I have this in Program.cs. My intent is that when userSession.OnIdentityChangedAsync has been fired (th

I have this in Program.cs. My intent is that when userSession.OnIdentityChangedAsync has been fired (the user is allowed to impersonate an identity) the cascading value of ActingIdentity is then refreshed throughout the app.

builder.Services.AddCascadingValue(
    sp =>
    {
        var userSession= sp.GetRequiredService<IUserSession<TPrincipal>>();

        var source = new CascadingValueSource<ActingIdentity<TPrincipal>>(
            "ActingIdentity", 
            ()=>userSession.ActingIdentity, 
            isFixed:false);

        var log = sp.GetRequiredService<Microsoft.Extensions.Logging.ILogger>();
        userSession.OnIdentityChangedAsync += async () =>
        {
            await source.NotifyChangedAsync();
            log.LogDebug("ActingIdentity Cascading Value Changed to {ActingIdentity}:{@source}", 
                userSession.ActingIdentity.ActingAs.Identity?.Name??"<null>",
                source);
        };
        return source;
    });

But what my logs show me is:

  1. The event handler you see here is called, so I am confident that source.NotifyChangedAsync() was called
  2. OnParametersSetAsync() does get called on a page that has a matching CascadingParameter.
  3. But the parameter value passed is stale. Only the initial value of the parameter is ever seen, subsequent changes are not passed on.

I'm on Net8 with InteractiveServer rendering:

    builder.Services
        .AddRazorComponents()
        .AddInteractiveServerComponents();

If instead of a root cascading value service I instead use <CascadingValue ...> markup in my Layout page, the changed value is passed to the page as expected.

Why doesn't my attempt at using AddCascadingValue() work?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743780682a4505741.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信