Информация об изменениях

Сообщение Re[6]: WebView2 с Proxy от 16.07.2025 12:08

Изменено 16.07.2025 12:09 Passerby

Re[6]: WebView2 с Proxy
Здравствуйте, bnk, Вы писали:

bnk>У меня он работает без прокси

Кто провайдер?

bnk>Ну я и говорю, ты уже где-то инициализируешь его до Form_Load (ставишь Source?)

Нет, Source не ставлю.
bnk>Ты сделал как написано выше, по шагам (1, 2)?
Сделал снова.
Новое приложение Form от Microsoft. Или нужен с FrameWork? Net 9.0.
Установил в NuGet WebView2.
Дополнил конструктор Form1.
К событию Load подсоединил метод Form1_Load(object sender, EventArgs e).
Запустил отладку, ошибка на строке await myWebView2.EnsureCoreWebView2Async(env);
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."
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        private readonly WebView2 myWebView2 = new WebView2();
        public Form1()
        {
            InitializeComponent();
            myWebView2.CreationProperties = null;
            myWebView2.Location = new Point(10, 65);
            myWebView2.Name = "BingWebView";
            myWebView2.Size = new Size(1750, 780);
            myWebView2.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
            myWebView2.TabIndex = 0;
            myWebView2.ZoomFactor = 1D;
            myWebView2.Visible = true;
            this.SuspendLayout();
            ClientSize = new Size(1800, 800);
            Controls.Add(myWebView2);
            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            var options = new CoreWebView2EnvironmentOptions
            {
                AdditionalBrowserArguments = "--proxy-server=http://5.161.131.126:8081"
            };
            var env = await CoreWebView2Environment.CreateAsync(null, null, options);
            await myWebView2.EnsureCoreWebView2Async(env);

            myWebView2.Source = new Uri("https://whatismyipaddress.com/");
        }
    }
}

namespace WinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            webView21 = new Microsoft.Web.WebView2.WinForms.WebView2();
            ((System.ComponentModel.ISupportInitialize)webView21).BeginInit();
            SuspendLayout();
            // 
            // webView21
            // 
            webView21.AllowExternalDrop = true;
            webView21.CreationProperties = null;
            webView21.DefaultBackgroundColor = Color.White;
            webView21.Location = new Point(24, 53);
            webView21.Name = "webView21";
            webView21.Size = new Size(764, 363);
            webView21.TabIndex = 0;
            webView21.ZoomFactor = 1D;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(800, 450);
            Controls.Add(webView21);
            Name = "Form1";
            Text = "Form1";
            Load += Form1_Load;
            ((System.ComponentModel.ISupportInitialize)webView21).EndInit();
            ResumeLayout(false);
        }

        #endregion

        private Microsoft.Web.WebView2.WinForms.WebView2 webView21;
    }
}
Re[6]: WebView2 с Proxy
Здравствуйте, bnk, Вы писали:

bnk>У меня он работает без прокси

Кто провайдер?

bnk>Ну я и говорю, ты уже где-то инициализируешь его до Form_Load (ставишь Source?)

Нет, Source не ставлю.
bnk>Ты сделал как написано выше, по шагам (1, 2)?
Сделал снова.
Новое приложение Form от Microsoft. Или нужен с FrameWork? Net 9.0.
Установил в NuGet WebView2.
Дополнил конструктор Form1.
К событию Load подсоединил метод Form1_Load(object sender, EventArgs e).
Запустил отладку, ошибка на строке await myWebView2.EnsureCoreWebView2Async(env);
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."

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        private readonly WebView2 myWebView2 = new WebView2();
        public Form1()
        {
            InitializeComponent();
            myWebView2.CreationProperties = null;
            myWebView2.Location = new Point(10, 65);
            myWebView2.Name = "BingWebView";
            myWebView2.Size = new Size(1750, 780);
            myWebView2.Source = new Uri("https://www.bing.com/", UriKind.Absolute);
            myWebView2.TabIndex = 0;
            myWebView2.ZoomFactor = 1D;
            myWebView2.Visible = true;
            this.SuspendLayout();
            ClientSize = new Size(1800, 800);
            Controls.Add(myWebView2);
            Name = "Form1";
            Text = "Form1";
            ResumeLayout(false);
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            var options = new CoreWebView2EnvironmentOptions
            {
                AdditionalBrowserArguments = "--proxy-server=http://5.161.131.126:8081"
            };
            var env = await CoreWebView2Environment.CreateAsync(null, null, options);
            await myWebView2.EnsureCoreWebView2Async(env);

            myWebView2.Source = new Uri("https://whatismyipaddress.com/");
        }
    }
}

namespace WinFormsApp1
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            webView21 = new Microsoft.Web.WebView2.WinForms.WebView2();
            ((System.ComponentModel.ISupportInitialize)webView21).BeginInit();
            SuspendLayout();
            // 
            // webView21
            // 
            webView21.AllowExternalDrop = true;
            webView21.CreationProperties = null;
            webView21.DefaultBackgroundColor = Color.White;
            webView21.Location = new Point(24, 53);
            webView21.Name = "webView21";
            webView21.Size = new Size(764, 363);
            webView21.TabIndex = 0;
            webView21.ZoomFactor = 1D;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 15F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(800, 450);
            Controls.Add(webView21);
            Name = "Form1";
            Text = "Form1";
            Load += Form1_Load;
            ((System.ComponentModel.ISupportInitialize)webView21).EndInit();
            ResumeLayout(false);
        }

        #endregion

        private Microsoft.Web.WebView2.WinForms.WebView2 webView21;
    }
}