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

RequestContext defines the cookie jar, cache partition, and preference scope for a browser. It must be assigned before the native CEF browser is created because CEF bakes the context into the browser at creation time. Setting it after browserCreated is true throws.

Source

Thrown at CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs:251

                {
                    browserSettings.Dispose();
                }

                browserSettings = value;
            }
        }
        /// <summary>
        /// Gets or sets the request context.
        /// </summary>
        /// <value>The request context.</value>
        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>
        /// Implement <see cref="IDialogHandler" /> and assign to handle dialog events.
        /// </summary>
        /// <value>The dialog handler.</value>
        public IDialogHandler DialogHandler { get; set; }
        /// <summary>
        /// Implement <see cref="IJsDialogHandler" /> and assign to handle events related to JavaScript Dialogs.
        /// </summary>
        /// <value>The js dialog handler.</value>

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Set RequestContext in XAML or in the constructor before adding the control to the visual tree.
  2. To use different contexts per session, create a fresh ChromiumWebBrowser instance with the desired RequestContext rather than reassigning.
  3. For per-request isolation, use ILoadHandler or IRequestHandler instead of swapping the context.

Example fix

// before — throws at runtime
browser.RequestContext = new RequestContext(new RequestContextSettings { CachePath = "profile2" });

// after — assign at construction
var browser = new ChromiumWebBrowser
{
    RequestContext = new RequestContext(new RequestContextSettings { CachePath = "profile2" })
};
Defensive patterns

Strategy: validation

Validate before calling

if (browser.IsBrowserInitialized)
{
    // too late to set RequestContext
    return;
}
browser.RequestContext = context;

Try / catch

try
{
    browser.RequestContext = context;
}
catch (Exception ex) when (ex.Message.Contains("already been created"))
{
    Log.Warn("RequestContext must be set before browser creation", ex);
}

Prevention

When it happens

Trigger: Assigning the RequestContext property after the control is loaded, after CreateBrowser, or once IsBrowserInitialized is true.

Common situations: Dynamically switching request contexts in response to user actions (e.g. login/logout, profile switching) after the browser is already running.

Related errors


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