{"record":{"id":"e7d1e8a405130189","repo":"cefsharp/CefSharp","slug":"requestcontext-has-already-been-initialized","errorCode":null,"errorMessage":"RequestContext has already been initialized, ","messagePattern":"RequestContext has already been initialized, ","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp/Handler/RequestContextHandler.cs","lineNumber":48,"sourceCode":"        {\n            this.onContextInitialziedAction = onContextInitialziedAction;\n\n            return this;\n        }\n\n        /// <summary>\n        /// Sets the preferences when the <see cref=\"IRequestContextHandler.OnRequestContextInitialized(IRequestContext)\"/>\n        /// method is called. If <paramref name=\"value\"/> is null the  preference will be restored\n        /// to its default value. Preferences set via the command-line usually cannot be modified.\n        /// </summary>\n        /// <param name=\"name\">preference name</param>\n        /// <param name=\"value\">preference value</param>\n        /// <returns>A <see cref=\"RequestContextHandler\"/> instance allowing you to chain multiple AddPreference calls together </returns>\n        public RequestContextHandler SetPreferenceOnContextInitialized(string name, object value)\n        {\n            if (requestContextInitialized)\n            {\n                throw new System.Exception(\"RequestContext has already been initialized, \");\n            }\n\n            preferences.Add(new KeyValuePair<string, object>(name, value));\n\n            return this;\n        }\n\n        /// <summary>\n        /// Sets the proxy preferences when the <see cref=\"IRequestContextHandler.OnRequestContextInitialized(IRequestContext)\"/>\n        /// method is called. Proxy set via the command-line usually cannot be modified.\n        /// </summary>\n        /// <param name=\"host\">proxy host</param>\n        /// <param name=\"port\">proxy port</param>\n        /// <returns>A <see cref=\"RequestContextHandler\"/> instance allowing you to chain multiple AddPreference calls together </returns>\n        public RequestContextHandler SetProxyOnContextInitialized(string host, int? port = null)\n        {\n            return SetProxyOnContextInitialized(null, host, port);\n        }","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp/Handler/RequestContextHandler.cs#L30-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before — called after context is already initialized\nhandler.SetPreferenceOnContextInitialized(\"webkit.webprefs.java_enabled\", true);\n// throws if requestContextInitialized is already true\n\n// after — set all preferences at build time, or use runtime API\n// at setup:\nvar handler = new RequestContextHandler()\n    .SetPreferenceOnContextInitialized(\"webkit.webprefs.java_enabled\", true);\nvar context = new RequestContext(new RequestContextSettings(), handler);\n\n// at runtime (after init):\ncontext.SetPreference(\"webkit.webprefs.java_enabled\", true, out string error);","handlingStrategy":"validation","validationCode":"// No public flag on RequestContextHandler — track initialization yourself.\n// For runtime preference changes, use the context API directly:\nif (requestContext != null)\n{\n    string error;\n    if (!requestContext.SetPreference(name, value, out error))\n        throw new InvalidOperationException($\"SetPreference failed: {error}\");\n}\n// Avoid SetPreferenceOnContextInitialized after init","typeGuard":null,"tryCatchPattern":"try\n{\n    handler.SetPreferenceOnContextInitialized(name, value);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"already been initialized\"))\n{\n    // Fall back to runtime preference API\n    string error;\n    requestContext.SetPreference(name, value, out error);\n}","preventionTips":["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."],"tags":["request-context","preferences","lifecycle","handler"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}