MicrosoftEdge / MicrosoftEdge/WebView2Feedback
[Problem/Bug]: System.Runtime.InteropServices.COMException in WebResourceResponseReceived event with lastest version of Edge
@vbryh-msft is already working on this.
Since Dec 13, 2023.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
What happened?
CoreWebview2 WebResourceResponseReceived event throws a ComException. We get data from a website in Json Format
and try to add it to our project.
We get the error message every time we add a product. In the previous version of the Browser there was no problem.
Customers cannot continue working now, please find a solution soon.
Problem exists in both our .NET Framework and .NET6 application.
private async void CoreWebView2_WebResourceResponseReceived(object? sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
try
{
if (e.Response.StatusCode != (int)HttpStatusCode.OK || !IsValidProduct(e.Request.Uri)) return;
using Stream content = await e.Response.GetContentAsync();
using StreamReader reader = new(content);
var data = await reader.ReadToEndAsync();
HandleData(data);
}
catch (COMException comException)
{
Debug.WriteLine($"===> Handle COMException {MethodBase.GetCurrentMethod().Name} : {comException.Message}");
}
catch (Exception exception)
{
Debug.WriteLine($"===> Handle Exception {MethodBase.GetCurrentMethod().Name} : {exception.Message}");
}
}
Importance
Blocking. My app's basic functions are not working due to this issue.
Runtime Channel
Stable release (WebView2 Runtime)
Runtime Version
120.0.2210.61
SDK Version
1.0.2210.55
Framework
Winforms
Operating System
Windows 11
OS Version
22621.2715
Repro steps
In a .NET6 Sample application add webview2 winforms control.
Using following code sample
using Microsoft.Web.WebView2.Core;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Webview2Sample
{
public partial class Webview : Form
{
public Webview()
{
InitializeComponent();
WebViewControl.CoreWebView2InitializationCompleted += WebViewControl_CoreWebView2InitializationCompleted;
InitializeAsync();
}
private void WebViewControl_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
{
WebViewControl.CoreWebView2.WebResourceResponseReceived += CoreWebView2_WebResourceResponseReceived;
WebViewControl.CoreWebView2.NavigationCompleted += CoreWebView2_NavigationCompleted;
}
private void CoreWebView2_NavigationCompleted(object? sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (!Enum.IsDefined(typeof(HttpStatusCode), e.HttpStatusCode)) return;
HttpStatusCode statusCode = Enum.IsDefined(typeof(HttpStatusCode), e.HttpStatusCode)
? (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), e.HttpStatusCode.ToString())
: HttpStatusCode.BadRequest;
if (statusCode != HttpStatusCode.OK) return;
//Debug.WriteLine($"Handle {MethodBase.GetCurrentMethod().Name} url: {WebViewControl.CoreWebView2.Source}");
}
private async void CoreWebView2_WebResourceResponseReceived(object? sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
{
try
{
if (e.Response.StatusCode != (int)HttpStatusCode.OK || !IsValidProduct(e.Request.Uri)) return;
using Stream content = await e.Response.GetContentAsync();
using StreamReader reader = new(content);
var data = await reader.ReadToEndAsync();
HandleData(data);
}
catch (COMException comException)
{
Debug.WriteLine($"===> Handle COMException {MethodBase.GetCurrentMethod().Name} : {comException.Message}");
}
catch (Exception exception)
{
Debug.WriteLine($"===> Handle Exception {MethodBase.GetCurrentMethod().Name} : {exception.Message}");
}
}
private static void HandleData(string data)
{
//Handle the product Json data.
}
private static bool IsValidProduct(string uri)
{
var isValidProduct = true; //Check if uri product data
return !string.IsNullOrEmpty(uri) && isValidProduct;
}
private async void InitializeAsync()
{
var myFolder = "\\Webview2Sample";
var userDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + myFolder;
CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(userDataFolder: userDataFolder);
await WebViewControl.EnsureCoreWebView2Async(env);
}
}
}
Add a navigation bar and an button to start the navigation, like so
```c#
private void NavigateButton_Click(object? sender, EventArgs e)
{
if (WebViewControl == null || WebViewControl.CoreWebView2 == null) return;
WebViewControl.CoreWebView2.Navigate(Url.Text);
}
I tried it with https://www.google.com. This gives the comException. in most cases.
Repros in Edge Browser
No
Regression
Regression in newer SDK
Last working version (if regression)
SDK 1.0.2210.55 Runtime 119.0.2151.44
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.