{"record":{"id":"3e45d46f8dfa4596","repo":"cefsharp/CefSharp","slug":"requestcontext-can-only-be-of-type-0-or-null","errorCode":null,"errorMessage":"RequestContext can only be of type {0} or null","messagePattern":"RequestContext can only be of type (.+?) or null","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.WinForms/ChromiumWebBrowser.cs","lineNumber":160,"sourceCode":"        public bool ActivateBrowserOnCreation { get; set; }\n        /// <summary>\n        /// Gets or sets the request context.\n        /// </summary>\n        /// <value>The request context.</value>\n        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)]\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        /// A flag that indicates whether the control is currently loading one or more web pages (true) or not (false).\n        /// </summary>\n        /// <value><c>true</c> if this instance is loading; otherwise, <c>false</c>.</value>\n        /// <remarks>In the WPF control, this property is implemented as a Dependency Property and fully supports data\n        /// binding.</remarks>\n        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(false)]\n        public bool IsLoading { get; private set; }\n        /// <summary>\n        /// The text that will be displayed as a ToolTip\n        /// </summary>\n        /// <value>The tooltip text.</value>\n        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue(null)]\n        public string TooltipText { get; private set; }","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms/ChromiumWebBrowser.cs#L142-L178","documentation":"Thrown by the WinForms ChromiumWebBrowser.RequestContext setter when the assigned value's UnWrap() result is not assignable to CefSharp.Core.RequestContext. As with BrowserSettings, the native bridge requires the exact core concrete type; a custom IRequestContext whose UnWrap() returns something else is rejected. The message names the expected type.","triggerScenarios":"Assigning a custom IRequestContext implementation whose UnWrap() does not return CefSharp.Core.RequestContext; passing a mock; mixing CefSharp assembly versions so the Core type identity differs.","commonSituations":"Unit tests with stub IRequestContext; version-mismatched CefSharp assemblies; wrapping the context in a decorator that does not UnWrap to the core type.","solutions":["Use `new CefSharp.Core.RequestContext(...)` (or ObjectFactory-created instance) rather than a custom implementation.","Align all CefSharp.* assembly versions so RequestContextType identity matches.","Call UnWrap() on your reference and verify it is the Core type before assigning.","Pass null for the default context instead of a foreign type."],"exampleFix":"// before\nbrowser.RequestContext = customCtx; // customCtx.UnWrap() is not Core.RequestContext\n\n// after\nbrowser.RequestContext = new RequestContext();","handlingStrategy":"type-guard","validationCode":"var rc = new RequestContext();\nif (rc.UnWrap() is CefSharp.Core.RequestContext)\n{\n    browser.RequestContext = rc;\n}","typeGuard":"static bool IsValidRequestContext(IRequestContext value)\n    => value == null || value.UnWrap() is CefSharp.Core.RequestContext;","tryCatchPattern":null,"preventionTips":["Only use RequestContext from CefSharp.Core / ObjectFactory.","Align all CefSharp.* assembly versions.","Verify UnWrap() returns the Core type when integrating third-party wrappers."],"tags":["cefwsharp","winforms","type-mismatch","request-context"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}