MicrosoftEdge / WebView2Feedback

Feedback and discussions about Microsoft Edge WebView2
https://aka.ms/webview2
441 stars 52 forks source link

Chrome DevTools Protocol Fetch.enable cannot receive RequestPattern parameters - System.ArgumentException #2842

Closed yanxiaodi closed 1 year ago

yanxiaodi commented 1 year ago

Description I would like to call Fetch.EnableAsync() method with a RequestPattern parameter but I got an error:

Exception thrown: 'System.ArgumentException' in System.Private.CoreLib.dll An exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.dll but was not handled in user code Value does not fall within the expected range.

Version SDK: WebView2: 1.0.1343.22, WebView2.DevToolsProtocolExtension: 1.0.824 Runtime: Framework: WPF OS: Win11, 22H2

Repro Steps

I have some code like this:

private DevToolsProtocolHelper devToolsProtocolHelper;
devToolsProtocolHelper = webView.CoreWebView2.GetDevToolsProtocolHelper();
...
var patterns = new RequestPattern[] { new RequestPattern { RequestStage = "Response" } };
// The below line throws an exception
await devToolsProtocolHelper.Fetch.EnableAsync(patterns);
// Also tried the code below but got the same error:
await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.enable", JsonSerializer.Serialize(patterns));

Screenshots image

Additional context

AB#41812449

chinasmu commented 1 year ago

In my winform app, I try this await CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.enable", "{\"patterns\":[{\"urlPattern\":\"*\", \"requestStage\":\"Response\"}]}");

It working good. About "RequestPattern[]", is it form CefSharp Common? I did not see any ref in webView2.

victorthoang commented 1 year ago

Hello @yanxiaodi ,

I have assigned this to @imicy, who will help figure out how to approach the bug you're seeing. Thanks for your find!

yanxiaodi commented 1 year ago

@chinasmu Thanks. I think I got it.

@imicy @victorthoang So if we call the method like

await CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.enable", "{\"patterns\":[{\"urlPattern\":\"*\", \"requestStage\":\"Response\"}]}");

It works. Because the parameter is an object that has a patterns field, which has the array as the value. But if we call the method like

var patterns = new RequestPattern[] { new RequestPattern { RequestStage = "Response" } };
// The below line throws an exception
await devToolsProtocolHelper.Fetch.EnableAsync(patterns);

The parameter is an array, not an object that has a patterns field.

yunate commented 1 year ago

@yanxiaodi, Hi, I'm looking into this bug. The reason is, RequestPattern's param cannot be null. You could try:

var patterns = new RequestPattern[] {
    new RequestPattern {
        UrlPattern = "",
        RequestStage = "Response",
        ResourceType = ""
    }
};

And about

await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.enable", JsonSerializer.Serialize(patterns));

has an another problem, you should use like this:

dynamic parameters = new ExpandoObject();
parameters.patterns = patterns;
await webView.CoreWebView2.CallDevToolsProtocolMethodAsync("Fetch.enable", JsonSerializer.Serialize(parameters));

The difference is :

[{"urlPattern":"","resourceType":"","requestStage":"Response"}] // your code generate
{"patterns":[{"urlPattern":"","resourceType":"","requestStage":"Response"}]} // expected
yanxiaodi commented 1 year ago

Thanks @yunate That's not an intuitive way to use this API. As a user, when I use await devToolsProtocolHelper.Fetch.EnableAsync(patterns);, the parameter has the RequestPattern[] type. Users would not know why it needs a ExpandoObject.

I think you can change the parameter type for this API?

yunate commented 1 year ago

@yanxiaodi thanks for your suggestion. About Users would not know why it needs a ExpandoObject. The reason is the CallDevToolsProtocolMethodAsync require its json string param must like {"patterns":[{"urlPattern":"","resourceType":"","requestStage":"Response"}]}.

About can change the parameter type for this API, we will talk about this with our team, but the origin API will be retained for version compatible.

champnic commented 1 year ago

The format of CDP API is outside the scope of WebView2 and is currently By Design.