Asp.net core 在blazor服务器端呈现页面时,如何停止IDisposable调用?

Asp.net core 在blazor服务器端呈现页面时,如何停止IDisposable调用?,asp.net-core,.net-core,blazor,Asp.net Core,.net Core,Blazor,这是我的密码: @page "/" @using Microsoft.AspNetCore.Components.Server.Circuits @inject CircuitHandler circuitHandler @implements IDisposable <h1>Hello, world!</h1> Welcome to your new app. @code{ protected override async Task OnAfterRend

这是我的密码:

@page "/"
@using Microsoft.AspNetCore.Components.Server.Circuits
@inject CircuitHandler circuitHandler
@implements IDisposable

<h1>Hello, world!</h1>

Welcome to your new app.

@code{
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            Console.WriteLine("123");//do something
        }
    }
    #region//circuitHandler
    protected override void OnInitialized()
    {
        // Subscribe to the event handler
        (circuitHandler as CircuitHandlerService).CircuitsChanged +=
             HandleCircuitsChanged;

    }


    public void HandleCircuitsChanged(object sender, EventArgs args)
    {
        Console.WriteLine("123");//do something
    }
    public void Dispose()
    {
        (circuitHandler as CircuitHandlerService).CircuitsChanged -=
          HandleCircuitsChanged;

    }
    #endregion
}
\u Host.cshtml

@(await Html.RenderComponentAsync<App>(RenderMode.Server))
@(等待Html.RenderComponentAsync(RenderMode.Server))
可以解决这个问题。

我试过了,但问题还在这里。


我怎样才能解决这个问题?谢谢。

下面的代码片段与您的代码片段完全相同,对我来说效果非常好。仅当回路闭合时才调用Dispose方法。我将在下面发布我使用的全部代码,复制并运行它,然后我将尝试帮助解决您遇到的其他问题

<app>
        @(await Html.RenderComponentAsync<App>(RenderMode.Server))
 </app>

@(等待Html.RenderComponentAsync(RenderMode.Server))
注意:我在这里发布整个文件的内容。你必须调整我的应用程序与你的应用程序的名称空间

CircuitHandlerService.cs的代码
使用Microsoft.AspNetCore.Components.Server.Circuits;
使用制度;
使用System.Collections.Concurrent;
使用系统线程;
使用System.Threading.Tasks;
命名空间blazorcuithisandler.Services
{
公共类CircuitHandler服务:CircuitHandler
{
公共ConcurrentDictionary电路{get;set;}
公共事件处理程序发生故障;
受保护的虚拟void OnCircuitsChanged()
=>CircuitsChanged?.Invoke(这个,EventArgs.Empty);
公共电路HandlerService()
{
电路=新的ConcurrentDictionary();
}
公用覆盖任务OnCircuitOpenedAsync(电路,取消令牌取消令牌)
{
电路[circuit.Id]=电路;
OnCircuitsChanged();
返回base.OnCircuitOpenedAsync(电路,取消令牌);
}
CircuitClosedAsync上的公共覆盖任务(电路,取消令牌取消令牌)
{
拆除电路;
Circuits.TryRemove(circuit.Id,out-circuit已移除);
OnCircuitsChanged();
返回base.OnCircuitClosedAsync(电路,取消令牌);
}
公共覆盖任务OnConnectionDownAsync(电路电路,CancellationToken CancellationToken)
{
返回base.OnConnectionDownAsync(电路,取消令牌);
}
公共覆盖任务OnConnectionUpAsync(电路电路,CancellationToken CancellationToken)
{
返回base.OnConnectionUpAsync(电路,取消令牌);
}
}
}
Index.razor的代码
@page/“
@使用Microsoft.AspNetCore.Components.Server.Circuits
@使用BlazorCircuitHandler.Services
@注入电路处理程序电路处理程序
@实现IDisposable

电路数:@((电路处理器为
BlazorCircuithHandler.Services.CircuitHandlerService.Circuits.Count)
    @foreach(var)电路输入(circuitHandler as BlazorCircuithHandler.Services.CircuitHandler.Service).Circuits) {
  • @电路,钥匙
  • }

@代码{ 受保护的覆盖无效OnInitialized() { (circuitHandler作为CircuitHandlerService)。CircuitsChanged+= 手电路改变; } 公共空间处置() { (circuitHandler作为CircuitHandlerService)。CircuitsChanged-= 手电路改变; } public void HandleCircuitsChanged(对象发送方、事件args args) { InvokeAsync(()=>StateHasChanged()); } }
还有创业班
public void配置服务(IServiceCollection服务)
{
//为简洁起见,删除了。。。。
AddSingleton(新的CircuitHandlerService());
}
当然还有_Host.cshtml

@(等待Html.RenderComponentAsync(RenderMode.Server))

请不要不报告…

下面的代码片段与您的代码片段完全相同,对我来说非常好。仅当回路闭合时才调用Dispose方法。我将在下面发布我使用的全部代码,复制并运行它,然后我将尝试帮助解决您遇到的其他问题

<app>
        @(await Html.RenderComponentAsync<App>(RenderMode.Server))
 </app>

@(等待Html.RenderComponentAsync(RenderMode.Server))
注意:我在这里发布整个文件的内容。你必须调整我的应用程序与你的应用程序的名称空间

CircuitHandlerService.cs的代码
使用Microsoft.AspNetCore.Components.Server.Circuits;
使用制度;
使用System.Collections.Concurrent;
使用系统线程;
使用System.Threading.Tasks;
命名空间blazorcuithisandler.Services
{
公共类CircuitHandler服务:CircuitHandler
{
公共ConcurrentDictionary电路{get;set;}
公共事件处理程序发生故障;
受保护的虚拟void OnCircuitsChanged()
=>CircuitsChanged?.Invoke(这个,EventArgs.Empty);
公共电路HandlerService()
{
电路=新的ConcurrentDictionary();
}
公用覆盖任务OnCircuitOpenedAsync(电路,取消令牌取消令牌)
{
电路[circuit.Id]=电路;
OnCircuitsChanged();
返回base.OnCircuitOpenedAsync(电路,取消令牌);
}
CircuitClosedAsync上的公共覆盖任务(电路,取消令牌取消令牌)
{
拆除电路;
Circuits.TryRemove(circuit.Id,out-circuit已移除);
OnCircuitsChanged();
返回base.OnCircuitClosedAsync(电路,取消令牌);
}
公共覆盖任务OnConnectionDownAsync(电路电路,CancellationToken CancellationToken)
{
返回base.OnConnectionDownAsync(电路,取消令牌);
}
公共覆盖任务OnConnectionUpAsync(电路电路,CancellationToken CancellationToken)
{
返回base.OnConnectionUpAsync(电路,取消令牌);
}
}
}
Index.razor的代码
@page/“
@使用Microsoft.AspNetCore.Components.Server.Circuits
@使用BlazorCircuitHandler.Services
@注入电路处理程序电路处理程序
@实现IDisposable

电路数:@((电路处理器为
BlazorCircuithHandler.Services.CircuitHandlerService.Circuits.Count)
    @foreach(var)电路输入(circuitHandler as BlazorCircuithHandler.Services.CircuitHandler.Service).Circuits) {
  • @巡回赛
    <app>
            @(await Html.RenderComponentAsync<App>(RenderMode.Server))
     </app>
    
    using Microsoft.AspNetCore.Components.Server.Circuits;
    using System;
    using System.Collections.Concurrent;
    using System.Threading;
    using System.Threading.Tasks;
    
    
    namespace BlazorCircuitHandler.Services
    {
    public class CircuitHandlerService : CircuitHandler 
    {
        public ConcurrentDictionary<string, Circuit> Circuits { get; set; }
        public event EventHandler CircuitsChanged;
    
        protected virtual void OnCircuitsChanged()
        => CircuitsChanged?.Invoke(this, EventArgs.Empty);
    
        public CircuitHandlerService()
        {
            Circuits = new ConcurrentDictionary<string, Circuit>();
        }
    
        public override Task OnCircuitOpenedAsync(Circuit circuit, CancellationToken cancellationToken)
        {
            Circuits[circuit.Id] = circuit;
            OnCircuitsChanged();
            return base.OnCircuitOpenedAsync(circuit, cancellationToken);
        }
    
        public override Task OnCircuitClosedAsync(Circuit circuit, CancellationToken cancellationToken)
        {
    
            Circuit circuitRemoved;
            Circuits.TryRemove(circuit.Id, out circuitRemoved);
            OnCircuitsChanged();
            return base.OnCircuitClosedAsync(circuit, cancellationToken);
        }
    
        public override Task OnConnectionDownAsync(Circuit circuit, CancellationToken cancellationToken)
        {
    
            return base.OnConnectionDownAsync(circuit, cancellationToken);
        }
    
        public override Task OnConnectionUpAsync(Circuit circuit, CancellationToken cancellationToken)
        {
            return base.OnConnectionUpAsync(circuit, cancellationToken);
        }
    
     }
    }
    
    @page "/"
    
    @using Microsoft.AspNetCore.Components.Server.Circuits
    @using BlazorCircuitHandler.Services
    
    @inject CircuitHandler circuitHandler
    @implements IDisposable
    
    
    <p>
        Number of Circuits: @((circuitHandler as 
        BlazorCircuitHandler.Services.CircuitHandlerService).Circuits.Count)
       <ul>
         @foreach (var circuit in (circuitHandler as 
                BlazorCircuitHandler.Services.CircuitHandlerService).Circuits)
         {
            <li>@circuit.Key</li>
         }
      </ul>
    </p>
    
    @code {
    
    protected override void OnInitialized()
    {
        (circuitHandler as CircuitHandlerService).CircuitsChanged += 
                       HandleCircuitsChanged;
    
    }
    
    public void Dispose()
    {
    
        (circuitHandler as CircuitHandlerService).CircuitsChanged -= 
                  HandleCircuitsChanged;
    
    }
    
    public void HandleCircuitsChanged(object sender, EventArgs args)
    {
        InvokeAsync(() => StateHasChanged());
    }
    }
    
    public void ConfigureServices(IServiceCollection services)
        {
            // Removed for brevity....
    
            services.AddSingleton<CircuitHandler>(new CircuitHandlerService());
        }
    
    <app>
            @(await Html.RenderComponentAsync<App>(RenderMode.Server))
    </app>