{"record":{"id":"e596cfa2b6cc45f8","repo":"cefsharp/CefSharp","slug":"requestcontext-can-only-be-of-type-0-or-null-e596cf","errorCode":null,"errorMessage":"RequestContext can only be of type {0} or null","messagePattern":"RequestContext can only be of type (.+?) or null","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf/ChromiumWebBrowser.cs","lineNumber":234,"sourceCode":"            }\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=\"IRenderHandler\"/> and control how the control is rendered\n        /// </summary>\n        /// <value>The render Handler.</value>\n        public IRenderHandler RenderHandler { get; set; }\n        /// <summary>\n        /// Implement <see cref=\"IAccessibilityHandler\" /> to handle events related to accessibility.\n        /// </summary>\n        /// <value>The accessibility handler.</value>\n        public IAccessibilityHandler AccessibilityHandler { get; set; }\n\n        /// <summary>\n        /// Raised every time <see cref=\"IRenderWebBrowser.OnPaint\"/> is called. You can access the underlying buffer, though it's\n        /// preferable to either override <see cref=\"OnPaint\"/> or implement your own <see cref=\"IRenderHandler\"/> as there is no outwardly","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf/ChromiumWebBrowser.cs#L216-L252","documentation":"Thrown by the WPF ChromiumWebBrowser.RequestContext setter when value.UnWrap() is not assignable to CefSharp.Core.RequestContext. The native bridge requires the exact core concrete type; a custom IRequestContext whose UnWrap() returns a different type is rejected. The message names the expected type for clarity. Mirror of the WinForms RequestContext type error.","triggerScenarios":"Assigning a custom IRequestContext implementation whose UnWrap() does not return CefSharp.Core.RequestContext; passing a mock/stub; mixing CefSharp assembly versions so the Core type identity differs.","commonSituations":"Unit tests with stub IRequestContext; version-mismatched CefSharp assemblies; decorator wrappers that do not UnWrap to the core type.","solutions":["Use `new CefSharp.Core.RequestContext(...)` rather than a custom implementation.","Align all CefSharp.* assembly versions so RequestContextType identity matches.","Verify value.UnWrap().GetType() is assignable to the Core type before assigning.","Pass null for the default context if you do not need isolation."],"exampleFix":"// before\nBrowser.RequestContext = customCtx; // UnWrap() is a foreign type\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 so the Core type identity matches.","Verify UnWrap() returns the Core type when integrating wrappers."],"tags":["cefwsharp","wpf","type-mismatch","request-context"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}