dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.33k stars 9.97k forks source link

clean up global event listener, when connection closed #57828

Open zuizuihao opened 4 weeks ago

zuizuihao commented 4 weeks ago

I found the ROOT CAUSE, @javiercn can we clean up the global event listener, when connection closed? when Blazor connection closed, the detachWebRendererInterop in the callback of connection.onclose will be invoked. here, it will invoke interopMethodsByRenderer.delete(rendererId);, all the interopMethodsByRenderer will be cleaned. also There are lots of global event listener in form.js like window.addEventListener('focus'), window.addEventListener('keydown'). if user still interact with our app like focus, click, the global event will be triggered, then invoked EventDelegator.onGlobalEvent, invoked EventDelegator.dispatchGlobalEventToAllElements invoked WebRendererInteropMethods.dispatchEvent invoked WebRendererInteropMethods.getInteropMethods, here since the interopMethodsByRenderer is empty, will throw this exception.

Originally posted by @zuizuihao in https://github.com/dotnet/aspnetcore/issues/56813#issuecomment-2345157777

javiercn commented 4 weeks ago

@zuizuihao thanks for contacting us.

Does this happen when you use MapRazorComponents and blazor.web.js or are you still relying on MapRazorComponents with blazor.server.js.

zuizuihao commented 4 weeks ago

nope, now we totally migrated into ASP.Net 8 using web.js. <script src="_framework/blazor.web.js?version=8"></script> in App.razor script body. endpoints.MapRazorComponents().AddInteractiveServerRenderMode()