Issues with WebView2 in WPF Usercontrol Embeded in Word VSTO CustomTaskPane

35 views Asked by At

I switched from WebBrowser control to WebView2 in my VSTO project, and I so much love the seamless rendering of pages using the updated control. However, I am having an issue with the automatic disposal of CustomTaskPaneCollections that happens when the application is closed.

The problem is that I get an exception whenever I close the Word application, mainly if the CustomTaskPane that has WebView2 embedded in it is open at least once during the last Word application process. It is a COMException, as shown in the image below:

enter image description here

It seems after the automatic CustomTaskPaneCollections Dispose() method, which is usually called whenever the Word application is closed and which causes all embedded child controls to be disposed of, the system still tries to access the WebView2 control, which keeps causing this exception to be raised.

How do I handle this issue, please? I've tried everything I could do. I am using WPF UserControl for embedding the WebView2 instead of Winfrom UserControl.

1

There are 1 answers

4
Nikolay On

If the problem is caused by you not properly releasing a COM reference, this dirty patch that could work:

private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
  GC.Collect();
  GC.WaitForPendingFinalizers();
  GC.Collect();
  GC.WaitForPendingFinalizers();
}