dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.2k stars 1.75k forks source link

"System.InvalidOperationException" exception occurs when closing the Blazer+Winform application #24995

Open takata1021 opened 1 month ago

takata1021 commented 1 month ago

Description

I am developing a Blazor+Winform app according to the site below. https://learn.microsoft.com/en-us/aspnet/core/blazor/hybrid/tutorials/windows-forms?view=aspnetcore-8.0

In the app I created, when I close the app from the Window's close button, a System.InvalidOperationException exception occurs.

The problem occurs with the code below (I also pasted the stack trace when the problem occurred). image image

The exception occurs after "Application.Run(form);" ends (after exiting from the Main function). It's probably the same cause as https://github.com/dotnet/maui/issues/7277, but I don't know how to work around it with Winform.

I tried the code below, but the problem remains the same.

private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
    mainBlazorView.RootComponents.Clear();
    Controls.Remove(mainBlazorView);
    mainBlazorView?.Dispose();
    mainBlazorView = null;
}

I am using .netsdk8.0.401

This problem occurs only occasionally.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

8.0.40 SR5

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

ninachen03 commented 3 weeks ago

Could you provide us with a sample project so we can investigate it further? Looking forward to your reply!