how to dispose the BlazorWebView in WPF blazor hybrid application?

William Liu 266 Reputation points
2024-04-06T01:15:06.14+00:00

I have a usercontrol which contains BlazorWebView control. After load/unload this usercontrol several times, I find there are more and more Microsoft Edge WebView2 appear in Windows task manager. I guess it was caused by the unreleased BlazorWebView resource. I also find the BlazorWebview is IAsyncDisposable, does it mean we should dispose it after using it? If so, please provide a demo to show how to dispose it.

I tried the following codes, it seems the code release the WebView2 resource sometime, but it will crush the WPF application if I load/unload the usercontrol too frequently.

public sealed partial class HomeView
{
    public HomeView()
    {
        InitializeComponent();

        Unloaded += HomeView_Unloaded;
    }

    private async void HomeView_Unloaded(object sender, System.Windows.RoutedEventArgs e)
    {
        await BlazorWebView.DisposeAsync();
    }
}

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,674 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,394 questions
{count} votes