最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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?

发布评论

评论列表(0)

  1. 暂无评论