cefsharp/CefSharp · error · ArgumentNullException
request
Error message
request
What it means
ArgumentNullException(nameof(request)) from the UrlRequest(IRequest request, IUrlRequestClient urlRequestClient, IRequestContext requestContext) constructor in the CefSharp wrapper. Creating an out-of-context URL request requires a non-null IRequest; null fails immediately. The request is unwrapped (request.UnWrap()) before being handed to the core constructor, so it must be a valid wrapper instance.
Source
Thrown at CefSharp.Core/UrlRequest.cs:45
{
}
/// <summary>
/// Create a new URL request that is not associated with a specific browser or frame.
/// Use <see cref="IFrame.CreateUrlRequest(IRequest, IUrlRequestClient)"/> instead if you want the
/// request to have this association, in which case it may be handled differently.
/// For requests originating from the browser process: It may be intercepted by the client via <see cref="IResourceRequestHandler"/> or <see cref="ISchemeHandlerFactory"/>.
/// POST data may only contain only a single element of type PDE_TYPE_FILE or PDE_TYPE_BYTES.
/// </summary>
/// <param name="request">request</param>
/// <param name="urlRequestClient">url request client</param>
/// <param name="requestContext">request context associated with this requets.</param>
public UrlRequest(IRequest request, IUrlRequestClient urlRequestClient, IRequestContext requestContext)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (urlRequestClient == null)
{
throw new ArgumentNullException(nameof(urlRequestClient));
}
urlRequest = new CefSharp.Core.UrlRequest(request.UnWrap(), urlRequestClient, requestContext?.UnWrap());
}
/// <inheritdoc/>
public bool ResponseWasCached
{
get { return urlRequest.ResponseWasCached; }
}
/// <inheritdoc/>
public IResponse ResponseView on GitHub (pinned to 16bc6e0711)
Solutions
- Construct the IRequest first (e.g. via frame.CreateRequest() or Cef.UiThreadRunFactory) and pass the non-null reference.
- Where possible prefer IFrame.CreateUrlRequest(IRequest, IUrlRequestClient) which associates the request with a frame and avoids manual construction.
- Guard the call site: skip UrlRequest creation if request is null and log the upstream source.
- Enable nullable reference types and annotate so the compiler flags the null.
Example fix
// before
var req = new UrlRequest(maybeNullRequest, client, ctx); // ArgumentNullException
// after
if (maybeNullRequest == null)
throw new InvalidOperationException("IRequest not yet materialised for this UrlRequest.");
var req = new UrlRequest(maybeNullRequest, client, ctx); Defensive patterns
Strategy: validation
Validate before calling
if (request == null) throw new InvalidOperationException("IRequest not materialised.");
var req = new UrlRequest(request, client, ctx); Type guard
public static bool IsUsableRequest(IRequest r) => r != null;
Try / catch
try { return new UrlRequest(request, client, ctx); }
catch (ArgumentNullException ex) when (ex.ParamName == "request")
{ /* construct the request first or skip */ throw; } Prevention
- Prefer IFrame.CreateUrlRequest over manual UrlRequest construction
- Build the IRequest via frame.CreateRequest() before this call
- Enable nullable reference types
When it happens
Trigger: Calling new UrlRequest(null, client, ctx); passing an IRequest obtained from a handler before it is materialised; reusing a request object after it was disposed so it surfaces as null via a helper.
Common situations: Building a custom UrlRequest from a resource request handler and forgetting to construct the IRequest; refactor where the request is now lazily created; code that assumes a callback always supplies a non-null IRequest.
Related errors
AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13).
Data as JSON: /api/errors/569151089ed21ac4.
Report an issue: GitHub.