cefsharp/CefSharp · error · Exception
RequestContext has already been initialized,
Error message
RequestContext has already been initialized,
What it means
RequestContextHandler.SetPreferenceOnContextInitialized queues a preference to be applied when OnRequestContextInitialized fires. Once the request context has been initialized (the requestContextInitialized flag is set), no further preferences can be queued through this helper — the initialization callback has already run and pending preferences are locked in.
Source
Thrown at CefSharp/Handler/RequestContextHandler.cs:48
{
this.onContextInitialziedAction = onContextInitialziedAction;
return this;
}
/// <summary>
/// Sets the preferences when the <see cref="IRequestContextHandler.OnRequestContextInitialized(IRequestContext)"/>
/// method is called. If <paramref name="value"/> is null the preference will be restored
/// to its default value. Preferences set via the command-line usually cannot be modified.
/// </summary>
/// <param name="name">preference name</param>
/// <param name="value">preference value</param>
/// <returns>A <see cref="RequestContextHandler"/> instance allowing you to chain multiple AddPreference calls together </returns>
public RequestContextHandler SetPreferenceOnContextInitialized(string name, object value)
{
if (requestContextInitialized)
{
throw new System.Exception("RequestContext has already been initialized, ");
}
preferences.Add(new KeyValuePair<string, object>(name, value));
return this;
}
/// <summary>
/// Sets the proxy preferences when the <see cref="IRequestContextHandler.OnRequestContextInitialized(IRequestContext)"/>
/// method is called. Proxy set via the command-line usually cannot be modified.
/// </summary>
/// <param name="host">proxy host</param>
/// <param name="port">proxy port</param>
/// <returns>A <see cref="RequestContextHandler"/> instance allowing you to chain multiple AddPreference calls together </returns>
public RequestContextHandler SetProxyOnContextInitialized(string host, int? port = null)
{
return SetProxyOnContextInitialized(null, host, port);
}View on GitHub (pinned to 16bc6e0711)
Solutions
- Call SetPreferenceOnContextInitialized only during setup, before the RequestContext is created or initialized.
- For runtime preference changes, use IRequestContext.SetPreference directly instead of the handler helper.
- If using the builder pattern, complete all SetPreferenceOnContextInitialized calls before assigning the handler to a RequestContext.
Example fix
// before — called after context is already initialized
handler.SetPreferenceOnContextInitialized("webkit.webprefs.java_enabled", true);
// throws if requestContextInitialized is already true
// after — set all preferences at build time, or use runtime API
// at setup:
var handler = new RequestContextHandler()
.SetPreferenceOnContextInitialized("webkit.webprefs.java_enabled", true);
var context = new RequestContext(new RequestContextSettings(), handler);
// at runtime (after init):
context.SetPreference("webkit.webprefs.java_enabled", true, out string error); Defensive patterns
Strategy: validation
Validate before calling
// No public flag on RequestContextHandler — track initialization yourself.
// For runtime preference changes, use the context API directly:
if (requestContext != null)
{
string error;
if (!requestContext.SetPreference(name, value, out error))
throw new InvalidOperationException($"SetPreference failed: {error}");
}
// Avoid SetPreferenceOnContextInitialized after init Try / catch
try
{
handler.SetPreferenceOnContextInitialized(name, value);
}
catch (Exception ex) when (ex.Message.Contains("already been initialized"))
{
// Fall back to runtime preference API
string error;
requestContext.SetPreference(name, value, out error);
} Prevention
- Call SetPreferenceOnContextInitialized only during setup, before the handler is assigned to a RequestContext.
- For runtime preference changes, use IRequestContext.SetPreference directly.
- Complete the builder chain (all SetPreferenceOnContextInitialized calls) before browser creation.
When it happens
Trigger: Calling SetPreferenceOnContextInitialized after the RequestContext has already fired its OnRequestContextInitialized callback — e.g. calling it from within that callback, or after the browser is already running with this handler.
Common situations: Adding preferences reactively (in response to user actions or events) after the browser/context is already live. Calling the method in a chain after the context initialized event has already been raised.
Related errors
- requestContextHandler
- Browser has already been created. RequestContext must be set
- Browser has already been created. RequestContext must be set
- Browser has already been created. RequestContext must be set
- IBrowser
AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13).
Data as JSON: /api/errors/e7d1e8a405130189.
Report an issue: GitHub.