cefsharp/CefSharp · error · Exception

Browser has already been created. RequestContext must be set

Error message

Browser has already been created. RequestContext must be set before the underlying CEF browser is created.

What it means

Thrown by the WinForms ChromiumWebBrowser.RequestContext setter when the underlying CEF browser has already been created. The RequestContext (cookie/cache隔离, proxy, preferences partition) is bound at browser creation time in CEF and cannot be changed afterward, so the setter rejects late assignment once browserCreated is true.

Source

Thrown at CefSharp.WinForms/ChromiumWebBrowser.cs:155

        /// Activates browser upon creation, the default value is false. Prior to version 73
        /// the default behaviour was to activate browser on creation (Equivalent of setting this property to true).
        /// To restore this behaviour set this value to true immediately after you create the <see cref="ChromiumWebBrowser"/> instance.
        /// https://github.com/chromiumembedded/cef/issues/1856
        /// </summary>
        public bool ActivateBrowserOnCreation { get; set; }
        /// <summary>
        /// Gets or sets the request context.
        /// </summary>
        /// <value>The request context.</value>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)]
        public IRequestContext RequestContext
        {
            get { return requestContext; }
            set
            {
                if (browserCreated)
                {
                    throw new Exception("Browser has already been created. RequestContext must be " +
                                        "set before the underlying CEF browser is created.");
                }
                if (value != null && !Core.ObjectFactory.RequestContextType.IsAssignableFrom(value.UnWrap().GetType()))
                {
                    throw new Exception(string.Format("RequestContext can only be of type {0} or null", Core.ObjectFactory.RequestContextType));
                }
                requestContext = value;
            }
        }
        /// <summary>
        /// A flag that indicates whether the control is currently loading one or more web pages (true) or not (false).
        /// </summary>
        /// <value><c>true</c> if this instance is loading; otherwise, <c>false</c>.</value>
        /// <remarks>In the WPF control, this property is implemented as a Dependency Property and fully supports data
        /// binding.</remarks>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(false)]
        public bool IsLoading { get; private set; }
        /// <summary>

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Assign RequestContext immediately after `new ChromiumWebBrowser(...)`, before the control is parented/shown.
  2. For per-session isolation, create the RequestContext first, build the browser with it, then parent the control.
  3. To change identity at runtime, dispose the current ChromiumWebBrowser and create a new one with a fresh RequestContext rather than reassigning the property.
  4. Avoid touching .Handle or adding to a visible parent before the assignment.

Example fix

// before
var browser = new ChromiumWebBrowser();
this.Controls.Add(browser);
browser.RequestContext = new RequestContext(new RequestContextSettings { CachePath = "profileA" }); // throws

// after
var rc = new RequestContext(new RequestContextSettings { CachePath = "profileA" });
var browser = new ChromiumWebBrowser { RequestContext = rc };
this.Controls.Add(browser);
Defensive patterns

Strategy: validation

Validate before calling

if (!browser.IsBrowserInitialized)
{
    browser.RequestContext = new RequestContext(new RequestContextSettings { CachePath = path });
}

Prevention

When it happens

Trigger: Setting RequestContext after the control handle was created (form shown, Controls.Add realized the handle, Load called, or handle recreated). Same window as the BrowserSettings error but for the request/identity context.

Common situations: Trying to switch a user/session identity (incognito, separate cookie jar, per-tab proxy) after the browser is already on screen; setting it in Form.Load; reassigning after navigation has started.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/f5cbf33e3d063b36. Report an issue: GitHub.