【问题标题】:Blazor Component changing for all users为所有用户更改 Blazor 组件
【发布时间】:2021-09-25 14:05:30
【问题描述】:

我们正在将 Blazor 整合到我们现有的 .net5.0 项目中。应用程序的单独部分正在使用 blazor。当用户进入该部分并通过单击链接更改视图时,所有用户的视图都将更改为相应的视图。

例如用户 A 正在查看页面 A,用户 B 转到页面 A,然后单击将组件更改为页面 B 的链接。用户 A 和用户 B 现在是 pageB。

项目位于 IIS 服务器上

ToCNode.razor

@using DataObjects.Help
@inject EditorState EditorState

<ul>
    @foreach (var child in Children)
    {
        <ToCNode wikiNodeDto="@child" />
    }
</ul>
@code {

    [Parameter]
    public WikiNodeDto wikiNodeDto { get; set; }
    IEnumerable<WikiNodeDto> Children { get; set; }
    string DisplayName { get; set; }
    bool ShowChildren { get; set; } = false;

    protected override void OnInitialized()
    {
        DisplayName = wikiNodeDto.Name;
        Children = wikiNodeDto.Children;
        base.OnInitialized();
    }
    void ToggleVisibility()
    {
        ShowChildren = !ShowChildren;
    }
    void SetContent()
    {
        EditorState.SetEditorContent(wikiNodeDto);
    }
}

Content.razor

@using Components
@using DataObjects.Help
@using Models.Dbo
@inject DataManagers.Interfaces.IHelpManager HelpManager
@inject EditorState EditorState
@implements IDisposable

<div class="container body-content" id="bodyContent">
    <div id="alertPlaceholder" style="margin-top:15px;"></div>
    <div class="row">
        <div class="col-xl-3" style="height:calc(100vh - 110px); overflow-y: auto; overflow-x: auto">
            <h3 style="text-align: center; color: #17a2b8;">Table of Contents</h3>
            <ToCNode wikiNodeDto="@nodes[0]" /> <!--root note-->
        </div>
        <div class="col-xl-9" style="height: calc(100vh - 110px); overflow-y: auto">
            <Editor savedContent="@EditorState.EditorContent" OnLoadArticle="@EditorState.OnLoadArticle" />
        </div>
    </div>
</div>

@code {

    public IList<WikiNodeDto> nodes;
    public string editorContent = "";

    protected override void OnInitialized()
    {
        nodes = HelpManager.GetAllWikiNodesDto();
        EditorState.wikiNodeDto = HelpManager.GetAllWikiNodesDto()[0];
        EditorState.EditorContent = EditorState.wikiNodeDto.Description;
        EditorState.OnLoadArticle = true;
        EditorState.OnChange += OnChangeHandler;
    }

    private async void OnChangeHandler()
    {
        await InvokeAsync(StateHasChanged);
    }

    public void Dispose()
    {
        EditorState.OnChange -= OnChangeHandler;
    }
}

EditorState.cs

using DataObjects.Help;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Components
{
    public class EditorState
    {
        public WikiNodeDto wikiNodeDto { get; set; }
        public string EditorContent { get; set; }
        public bool OnLoadArticle { get; set; }

        public event Action OnChange;

        public void SetEditorContent(WikiNodeDto nodeDto)
        {
            wikiNodeDto = nodeDto;
            EditorContent = nodeDto.Description;
            OnLoadArticle = true;
            NotifyStateChanged();
        }

        public void NotifyStateChanged()
        {
            OnChange?.Invoke();
        }
    }
}

Startup.cs

services.AddSingleton<EditorState>(); //this was the issue
services.AddScoped<EditorState>(); // This is what it should be

编辑:更新了更多信息

第二次编辑:使用 Startup.cs 更新

【问题讨论】:

  • 我们仍然需要查看 Startup.cs 中您注册 EditorState 的那一行。
  • 用答案更新了问题。感谢您的帮助!
  • 所有这些答案都假定服务器的相同实例。并非在所有托管环境中都是如此。所以要小心在 dev 中有效的东西可能在 prod 中无效。
  • @BrianParker - 我还没有看到任何 Blazor 服务器横向扩展的示例。你认为这可能吗?
  • @BrianParker - 在这种情况下,它是服务器的同一个实例。该项目托管在 IIS 服务器上,并在内部网络中使用。

标签: c# blazor blazor-server-side


【解决方案1】:

我猜一下:EditorState 是一个单例实例。由所有用户共享。

您应该添加有关如何创建和管理它的代码。现在还不清楚它是注入的还是级联的还是只是静态的。

【讨论】:

    【解决方案2】:

    根据@Henk Holterman 的说法,问题在于 EditorState 作为 Singleton 添加到启动中。

    已将其更改为 AddScoped 并且可以正常工作。

    【讨论】:

    • 我想指出,这个“问题”实际上是 Blazor 最令人敬畏的事情之一。在所有用户之间共享一个类实例?我的意思是——潜力是无限的。我已经制作了一个全球弹出系统,它将向所有在线的人显示消息。
    • 是的!完全认为这是 Blazor 的一个很棒的功能。只需要记住/注意这一点,因为在这种情况下,它指的是一篇文章,所有用户在查看时都会为他们更改文章。
    • 我想每个新的 Blazor 程序员在他们第一次上线他们的网站时至少会发生一次这种情况:“为什么 Jenny 的 ChangeAddress 表单显示了 Johnny 的所有个人信息?” :D
    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2021-05-09
    • 2019-11-08
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多