{"record":{"id":"bd7560dda64f6eaf","repo":"cefsharp/CefSharp","slug":"requestcontext-can-only-be-of-type-0-or-null-bd7560","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/HwndHost/ChromiumWebBrowser.cs","lineNumber":256,"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=\"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>\n        public IJsDialogHandler JsDialogHandler { get; set; }\n        /// <summary>\n        /// Implement <see cref=\"IKeyboardHandler\" /> and assign to handle events related to key press.\n        /// </summary>\n        /// <value>The keyboard handler.</value>","sourceCodeStart":238,"sourceCodeEnd":274,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs#L238-L274","documentation":"The RequestContext property only accepts instances whose unwrapped type is assignable to Core.ObjectFactory.RequestContextType — the internal CefSharp implementation. A custom IRequestContext implementation or a mock that does not derive from the correct concrete type is rejected.","triggerScenarios":"Passing a hand-rolled IRequestContext, a mocking-framework proxy, or any wrapper whose UnWrap() returns something that is not the Core RequestContext type.","commonSituations":"Unit testing with a mocked IRequestContext. Building a decorator/wrapper around IRequestContext without inheriting from RequestContext.","solutions":["Use RequestContext or RequestContextAdapter from CefSharp.Core — instantiate via new RequestContext(settings) or Cef.GetGlobalRequestContext().","If wrapping is needed, inherit from RequestContext and override virtual members rather than implementing IRequestContext from scratch.","In tests, use a real RequestContext with a temp cache path instead of a mock."],"exampleFix":"// before — custom implementation rejected\npublic class MyRequestContext : IRequestContext { ... }\nbrowser.RequestContext = new MyRequestContext();\n\n// after — use the Core type\nbrowser.RequestContext = new RequestContext(new RequestContextSettings { CachePath = cachePath });","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Verify the unwrapped type is the Core RequestContext before assigning\nstatic bool IsValidRequestContext(IRequestContext context)\n{\n    if (context == null) return true; // null is allowed\n    var unwrapped = context.UnWrap();\n    return CefSharp.Core.ObjectFactory.RequestContextType\n        .IsAssignableFrom(unwrapped.GetType());\n}","tryCatchPattern":"try\n{\n    browser.RequestContext = context;\n}\ncatch (Exception ex) when (ex.Message.Contains(\"RequestContext can only be of type\"))\n{\n    throw new InvalidOperationException(\n        \"Use RequestContext from CefSharp.Core, not a custom IRequestContext\", ex);\n}","preventionTips":["Always instantiate RequestContext via `new RequestContext(...)` from CefSharp.Core.","Do not mock IRequestContext in integration tests — use a real RequestContext with a temp cache.","If wrapping is needed, inherit from RequestContext, not implement IRequestContext."],"tags":["request-context","type-validation","wpf-hwndhost"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}