WebView2 CallDevToolsProtocolMethodAsync issue with Fetch.continueRequest

0

I'm using WebView2 in a .net 5 WPF app and have been playing with the devtools protocol as a means of intercepting specific requests for assets. In looking at the Chrome dev docs (https://chromedevtools.github.io/devtools-protocol/), it's possible to intercept requests and then decide whether to continue them, cancel them or satisfy them yourself.

I've been able to successfully intercept the first web request (Eg. https:// www.somedomain.tld), but I've not been able to successfully continue the request (which would presumably trigger any other asset requests made as a result of the parsed html response).

After WebView initialization, I do the following (which works):

// Intercept requests
var receiver = webView.CoreWebView2.GetDevToolsProtocolEventReceiver("Fetch.requestPaused");
receiver.DevToolsProtocolEventReceived += FetchRequestPaused;
await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.enable", "{}");

This is my event handler - which doesn't do what I'm expecting it to (although it doesn't deadlock now at least):

private void FetchRequestPaused(object sender, Microsoft.Web.WebView2.Core.CoreWebView2DevToolsProtocolEventReceivedEventArgs e)
{
   var doc = JsonDocument.Parse(e.ParameterObjectAsJson);
   var id = doc.RootElement.GetProperty("requestId");
   var payload = $"{{\"requestId\":\"{id}.0\"}}";

   // We can't do this as an async call as it will try to post to the main thread, which is
   // busy waiting in this event handler, so we deadlock
   //_ = await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.continueRequest", payload);

   // Exception: Value does not fall within the expected range.
   // var result = await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.continueRequest", payload).ConfigureAwait(false);

   // PROBLEM: This invokes the call on the UI thread OK...
   Application.Current.Dispatcher.Invoke(new Action(() =>
   {
      // ...but it doesn't actually do anything
      webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.continueRequest", payload);
   }));
}

Not only does the requested page not finish loading, but the browser is left in an unusual state and so right-clicking on the control and selecting "Refresh" will crash - yielding a COMException:

System.Runtime.InteropServices.COMException: 'The group or resource is not in the correct state to perform the requested operation. (0x8007139F)'

Can anyone see what I'm doing wrong here or am missing??

Thanks!

Additional information

In swapping out the events for the deprecated Network.setRequestInterception / Network.continueInterceptedRequest equivalents, I'm seeing the same behaviour - which at least tells us that it's either a problem with my calling code (most likely) or a bug in WebView2 (possible) rather than Chromium.

Any thoughts?

c#
chromium
webview2
asked on Stack Overflow Mar 4, 2021 by gplumb • edited Mar 4, 2021 by gplumb

1 Answer

0

After some more digging, I realised there were two problems. The first is that my installed version of Edge was slightly behind. The second was that my Action delegate was synchronous. The call should read:

// Somebody forgot some 'async' keywords..!
Application.Current.Dispatcher.Invoke(new Action(async () =>
{
   var x = await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.continueRequest", payload);
}));
answered on Stack Overflow Mar 5, 2021 by gplumb

User contributions licensed under CC BY-SA 3.0