{"record":{"id":"ab4bd4bec5876b5e","repo":"cefsharp/CefSharp","slug":"browser-has-already-been-created-requestcontext-m-ab4bd4","errorCode":null,"errorMessage":"Browser has already been created. RequestContext must be set before the underlying CEF browser is created.","messagePattern":"Browser has already been created\\. RequestContext must be set before the underlying CEF browser is created\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs","lineNumber":251,"sourceCode":"                {\n                    browserSettings.Dispose();\n                }\n\n                browserSettings = value;\n            }\n        }\n        /// <summary>\n        /// Gets or sets the request context.\n        /// </summary>\n        /// <value>The request context.</value>\n        public IRequestContext RequestContext\n        {\n            get { return requestContext; }\n            set\n            {\n                if (browserCreated)\n                {\n                    throw new Exception(\"Browser has already been created. RequestContext must be \" +\n                                        \"set before the underlying CEF browser is created.\");\n                }\n                if (value != null && !Core.ObjectFactory.RequestContextType.IsAssignableFrom(value.UnWrap().GetType()))\n                {\n                    throw new Exception(string.Format(\"RequestContext can only be of type {0} or null\", Core.ObjectFactory.RequestContextType));\n                }\n                requestContext = value;\n            }\n        }\n        /// <summary>\n        /// Implement <see cref=\"IDialogHandler\" /> and assign to handle dialog events.\n        /// </summary>\n        /// <value>The dialog handler.</value>\n        public IDialogHandler DialogHandler { get; set; }\n        /// <summary>\n        /// Implement <see cref=\"IJsDialogHandler\" /> and assign to handle events related to JavaScript Dialogs.\n        /// </summary>\n        /// <value>The js dialog handler.</value>","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs#L233-L269","documentation":"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.","triggerScenarios":"Assigning the RequestContext property after the control is loaded, after CreateBrowser, or once IsBrowserInitialized is true.","commonSituations":"Dynamically switching request contexts in response to user actions (e.g. login/logout, profile switching) after the browser is already running.","solutions":["Set RequestContext in XAML or in the constructor before adding the control to the visual tree.","To use different contexts per session, create a fresh ChromiumWebBrowser instance with the desired RequestContext rather than reassigning.","For per-request isolation, use ILoadHandler or IRequestHandler instead of swapping the context."],"exampleFix":"// before — throws at runtime\nbrowser.RequestContext = new RequestContext(new RequestContextSettings { CachePath = \"profile2\" });\n\n// after — assign at construction\nvar browser = new ChromiumWebBrowser\n{\n    RequestContext = new RequestContext(new RequestContextSettings { CachePath = \"profile2\" })\n};","handlingStrategy":"validation","validationCode":"if (browser.IsBrowserInitialized)\n{\n    // too late to set RequestContext\n    return;\n}\nbrowser.RequestContext = context;","typeGuard":null,"tryCatchPattern":"try\n{\n    browser.RequestContext = context;\n}\ncatch (Exception ex) when (ex.Message.Contains(\"already been created\"))\n{\n    Log.Warn(\"RequestContext must be set before browser creation\", ex);\n}","preventionTips":["Set RequestContext at construction time (in XAML or the object initializer).","For multi-profile apps, create a new ChromiumWebBrowser per profile rather than reassigning.","Treat RequestContext as immutable after construction."],"tags":["request-context","lifecycle","validation","wpf-hwndhost"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}