-1

I can make the proxy setting of Webview2 at the first initialize but I need to change them multiple time during runtime, when I try to do it again I get this error

System.ArgumentException: 'WebView2 was already initialized with a different CoreWebView2Environment. Check to see if the Source property was already set or EnsureCoreWebView2Async was previously called with different values.'

How can I do that? My code is:

        Dim options As CoreWebView2EnvironmentOptions = New CoreWebView2EnvironmentOptions With {.Language = "en-GB"}

        options.AdditionalBrowserArguments = "--proxy-server=geo.xxxxx.com:12345"
        Dim webView2Environment As CoreWebView2Environment = Nothing
        webView2Environment = Await CoreWebView2Environment.CreateAsync(Nothing, Nothing, options)
        Await WebView21.EnsureCoreWebView2Async(webView2Environment)

I tried the above code and it did not work

Taner
  • 3
  • 3
  • Why would one need to constantly be changing one's proxy server? WebView2 wasn't designed to be useful for people engaging in illicit activities. – Tu deschizi eu inchid Mar 11 '23 at 19:28
  • If your ip is blocked after a certain time, then you need to change it, just like rotating proxies. – Taner Mar 12 '23 at 01:19
  • What are you doing such that your IP is _blocked after a certain time_? I've been using the internet for a while and to my knowledge my IP has never been blocked. Seems like the better solution is to not participate in activities that result in your IP address being blocked. – Tu deschizi eu inchid Mar 12 '23 at 02:27

1 Answers1

0

You cannot change the CoreWebView2EnvironmentOptions.AdditionalBrowserArguments for an already running WebView2 or associated WebView2 browser process or CoreWebView2Environment. You'll need to either:

  • Close the WebView2 and associated CoreWebView2Environment before trying to create a new one using a modified CoreWebView2EnvironmentOptions
  • Create a new and different CoreWebView2Environment that has a different user data folder than the already running CoreWebView2Environment.
  • Request a feature from the WebView2 team to support modifying the proxy settings of a running WebView2.

The CoreWebView2EnvironmentOptions are the options used to create a CoreWebView2Environment and associated WebView2 browser process. Because they are used during creation of the associated WebView2 browser process, you cannot change them later. This is also true if you create multiple CoreWebView2Environment objects with the same user data folder. Such CoreWebView2Environment objects all share the same user data folder and the same WebView2 browser process and so cannot have different creation options.

Specifically for the case described, the proxy settings are a part of the CoreWebView2EnvironmentOptions.AdditionalBrowserArguments which is used as a part of the command line when starting the WebView2 msedgewebview2.exe browser process. Accordingly, we can't change the command line after starting the process. You need to either close that WebView2 and start it again with modified command line arguments or create a new WebView2 with a different user data folder so that it doesn't try to share the browser process with the already running WebView2. Alternatively, you can ask the WebView2 team to support modifying proxy settings for a running WebView2.

You can read more about the WebView2 process model.

David Risney
  • 3,886
  • 15
  • 16