cefsharp/CefSharp · error · Exception
RequestContext can only be of type {0} or null
Error message
RequestContext can only be of type {0} or null What it means
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.
Source
Thrown at CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs:256
}
}
/// <summary>
/// Gets or sets the request context.
/// </summary>
/// <value>The request context.</value>
public IRequestContext RequestContext
{
get { return requestContext; }
set
{
if (browserCreated)
{
throw new Exception("Browser has already been created. RequestContext must be " +
"set before the underlying CEF browser is created.");
}
if (value != null && !Core.ObjectFactory.RequestContextType.IsAssignableFrom(value.UnWrap().GetType()))
{
throw new Exception(string.Format("RequestContext can only be of type {0} or null", Core.ObjectFactory.RequestContextType));
}
requestContext = value;
}
}
/// <summary>
/// Implement <see cref="IDialogHandler" /> and assign to handle dialog events.
/// </summary>
/// <value>The dialog handler.</value>
public IDialogHandler DialogHandler { get; set; }
/// <summary>
/// Implement <see cref="IJsDialogHandler" /> and assign to handle events related to JavaScript Dialogs.
/// </summary>
/// <value>The js dialog handler.</value>
public IJsDialogHandler JsDialogHandler { get; set; }
/// <summary>
/// Implement <see cref="IKeyboardHandler" /> and assign to handle events related to key press.
/// </summary>
/// <value>The keyboard handler.</value>View on GitHub (pinned to 16bc6e0711)
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.
Example fix
// before — custom implementation rejected
public class MyRequestContext : IRequestContext { ... }
browser.RequestContext = new MyRequestContext();
// after — use the Core type
browser.RequestContext = new RequestContext(new RequestContextSettings { CachePath = cachePath }); Defensive patterns
Strategy: type-guard
Type guard
// Verify the unwrapped type is the Core RequestContext before assigning
static bool IsValidRequestContext(IRequestContext context)
{
if (context == null) return true; // null is allowed
var unwrapped = context.UnWrap();
return CefSharp.Core.ObjectFactory.RequestContextType
.IsAssignableFrom(unwrapped.GetType());
} Try / catch
try
{
browser.RequestContext = context;
}
catch (Exception ex) when (ex.Message.Contains("RequestContext can only be of type"))
{
throw new InvalidOperationException(
"Use RequestContext from CefSharp.Core, not a custom IRequestContext", ex);
} Prevention
- 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.
When it happens
Trigger: Passing a hand-rolled IRequestContext, a mocking-framework proxy, or any wrapper whose UnWrap() returns something that is not the Core RequestContext type.
Common situations: Unit testing with a mocked IRequestContext. Building a decorator/wrapper around IRequestContext without inheriting from RequestContext.
Related errors
- Browser has already been created. RequestContext must be set
- otherRequestContext
- requestContextHandler
- settings
- A call to WithSharedSettings has already been made, it is no
AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13).
Data as JSON: /api/errors/bd7560dda64f6eaf.
Report an issue: GitHub.