ali-1997
ali-1997

Reputation: 1

How can I create multiple webview2 browsers with different proxies in one app?

I'm trying to create a c# windows form that has a simple task which is to display two separate browsers each one has its own server proxy, but no matter what I do I get this error:

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

How can I achieve that task using c# webview2?

this is the code that I've tried:

using System;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;

namespace WebView2MultipleProxies
{
    public partial class Form1 : Form
    {
        private WebView2 webView21;
        private WebView2 webView22;

        public Form1()
        {
            InitializeComponent();
            InitializeWebView1();
        }

private async void InitializeWebView1()
{
    try
    {
        // Create an environment with Proxy 1
        var options1 = new CoreWebView2EnvironmentOptions("--proxy-server=address:port");
        var environment1 = await CoreWebView2Environment.CreateAsync(null, null, options1);

        // Initialize WebView2 with the first environment
        webView21 = new WebView2
        {
            Width =500,
            Height =500,
            Top =100,
            Left =100
        };

        Controls.Add(webView21);
        await webView21.EnsureCoreWebView2Async(environment1);

        webView21.Source = new Uri("https://www.google.com");

        // After WebView1 is initialized, initialize WebView2
        await InitializeWebView2();
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error initializing WebView1: {ex.Message}");
    }
}

private async Task InitializeWebView2()
{
   // webView21.Dispose();
    try
    {
        // Create an environment with Proxy 2
        var options2 = new CoreWebView2EnvironmentOptions("--proxy-server=address:port");
        var environment2 = await CoreWebView2Environment.CreateAsync(null, null, options2);

        // Initialize WebView2 with the second environment
        webView22 = new WebView2
        {
            Width = 500,
            Height = 500,
            Top = 100,
            Left = 650
        };
        Controls.Add(webView22);
        await webView22.EnsureCoreWebView2Async(environment2);
        webView22.Source = new Uri("https://www.google.com");
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Error initializing WebView2: {ex.Message}");
    }
  }
 }
}

Upvotes: 0

Views: 18

Answers (0)

Related Questions