Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to avoid exception of Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost in blazor server side?

$
0
0

I am running Blazor web application which is server side render mode. The application works to monitor database changed and render the changed info to browser.

The code of watch db changed and notify browser is blow:

    public async Task WatchAppLogChangesAsync(CancellationToken cancellationToken)    {        var pipeline = new EmptyPipelineDefinition<ChangeStreamDocument<AppLog>>().Match(x => x.OperationType == ChangeStreamOperationType.Insert);        using (var cursor = await _appLogsCollection.WatchAsync(pipeline))        {            await cursor.ForEachAsync(async change =>            {                var playerId = change.FullDocument.UserId;                if (_playerClientMap.TryGetValue(playerId, out var clients))                {                    foreach (var clientId in clients)                    {                        Console.WriteLine($"tell client: {clientId} when app log changed: {change.FullDocument.Message}");                        await _hubContext.Clients.Client(clientId).SendAsync("AppLogUpdated", $"{change.FullDocument.Message}\r\n{change.FullDocument.RawData}");                    }                }            });        }    }

The code to render log is as following:

    protected override async Task OnAfterRenderAsync(bool firstRender)    {        if (!firstRender)            return;        hubConnection = new HubConnectionBuilder()        .WithUrl(Navigation.ToAbsoluteUri("/chathub"))        .Build();        hubConnection.On<string>("AppLogUpdated", async (logMsg) =>        {            @* Console.WriteLine($"receive from hub2: {logMsg}"); *@            messages.Add(logMsg);            await InvokeAsync(StateHasChanged);        });        await hubConnection.StartAsync();        await Send();        await GetLatestLogs(2);    }

When a large number of logs are suddenly inserted, I can get the following excepiton:

warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]      Unhandled exception rendering component: Collection was modified; enumeration operation may not execute.      System.InvalidOperationException: Collection was modified; enumeration operation may not execute.         at System.Collections.Generic.List`1.Enumerator.MoveNextRare()         at System.Collections.Generic.List`1.Enumerator.MoveNext()         at Radzen.Blazor.Rendering.Markers`1.BuildRenderTree(RenderTreeBuilder __builder)         at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)         at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)         at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)         at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]      Unhandled exception in circuit 'ZqYGFNtGl3fN67EQprc7TtLm6bTk1DNfRUOpqxgDQJQ'.      System.InvalidOperationException: Collection was modified; enumeration operation may not execute.         at System.Collections.Generic.List`1.Enumerator.MoveNextRare()         at System.Collections.Generic.List`1.Enumerator.MoveNext()         at Radzen.Blazor.Rendering.Markers`1.BuildRenderTree(RenderTreeBuilder __builder)         at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)         at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)         at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)         at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]

I think this problem may be related log was added when the server render the code, but I don't know the specific details that cause this problem.Does anyone know how to solve this problem?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>