Repository navigation
Closed
Labels
bugSomething isn't working
Description
An InvalidCastException occurs when trying to set a new CoreWebView2 instance to CoreWebView2NewWindowRequestedEventArgs.NewWindow in the CoreWebView2.NewWindowRequested event in WPF.
Microsoft.Web.WebView2 0.9.515-prerelease
Version 84.0.524.0 (Official build) canary (64-bit)
Visual Studio 2019 Version 16.7.0
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Web.WebView2.Core.Raw.ICoreWebView2NewWindowRequestedEventArgs'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{9EDC7F5F-C6EA-4F3C-827B-A8880794C0A9}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at Microsoft.Web.WebView2.Core.Raw.ICoreWebView2NewWindowRequestedEventArgs.set_NewWindow(ICoreWebView2 NewWindow)
at Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs.set_NewWindow(CoreWebView2 value)
at MultithreadedWebView.BrowserWindow.<>c__DisplayClass43_0.<ModernBrowser_NewWindowRequested>b__0() in E:\Visual Studio 2019\Projects\MultithreadedWebView\MultithreadedWebView\BrowserWindow.xaml.cs:line 147
Some of the code is as follows.
private void ModernBrowser_NewWindowRequested(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs e)
{
NewWindowRequestedEventArgs args = new NewWindowRequestedEventArgs(this.TabPageIndex);
Microsoft.Web.WebView2.Core.CoreWebView2Deferral deferral = e.GetDeferral();
OnNewWindowRequested(args);
if ((args.Handled == true) && (args.NewWindow != null)) {
args.NewWindow.Dispatcher.Invoke(new Action(() => {
try {
e.NewWindow = args.NewWindow.ModernBrowser.CoreWebView2;
e.Handled = true;
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}));
deferral.Complete();
}
}
The full code is available on GitHub.
Steps to repro
- Launch the sample app and navigate to https://edgewatcher.azurewebsites.net/jstest.htm.
- Click [Open] button in the page to open a new window.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Activity
david-risney commentedon May 26, 2020
Thanks for reporting this issue and including the source for the repro! We'll look into this.
ms-yuhara commentedon May 27, 2020
Thanks! I think this is essentially a threading and marshalling issue. In this sample, the parent and child windows are running in different threads, and the handler of the NewWindowRequested event is attempting to set a CoreWebView2 instance that was created in a different thread. What are the best practices in this multi-threaded WebView2 scenario?
ms-yuhara commentedon Aug 20, 2020
David Risney (@david-risney) Any update on this? Is WebView2 .NET using in multi-thread not supported?
champnic commentedon Sep 3, 2020
ms-yuhara Correct - WebView2 should only be accessed from a UI thread, and a given WebView2 should only be created and accessed on a single thread: https://docs.microsoft.com/en-us/microsoft-edge/webview2/concepts/threading-model The error you are seeing is by design.
Can you explain a bit more about why you are trying to manually set the CoreWebView2? If you create a new WebView2 and it uses the same user data folder, it should reuse all of the same WebView processes.
ms-yuhara commentedon Sep 3, 2020
Nic Champagne Williamson [MSFT] (@champnic) Thank you for providing the answer! I understand that WebView2 should be created on the same UI thread.
I'm creating a new instance of WebView2 because when a page requests a new window (e.g. window.open), I want to open the page in my app's own window instead of the Edge's window. This approach is the same as the one described in https://docs.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/0-9-538/icorewebview2#add_newwindowrequested and is considered supported. Correct?
champnic commentedon Sep 3, 2020
Ah sorry yes, that part is correct. I just tried your code and looks like it's already fixed! I'm going to close this issue now. If you have more problems in this area please reopen, or open another issue. Thanks!!