【发布时间】: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