cefsharp/CefSharp · error · ArgumentNullException

otherRequestContext

Error message

otherRequestContext

What it means

ArgumentNullException(nameof(otherRequestContext)) from the RequestContext(IRequestContext otherRequestContext) constructor in the CefSharp wrapper assembly. This constructor shares storage with an existing request context; passing null has no valid meaning, so it fails fast with the parameter name 'otherRequestContext'.

Source

Thrown at CefSharp.Core/RequestContext.cs:30

namespace CefSharp
{
    /// <inheritdoc/>
    public class RequestContext : IRequestContext
    {
        private CefSharp.Core.RequestContext requestContext;

        /// <inheritdoc/>
        public RequestContext()
        {
            requestContext = new CefSharp.Core.RequestContext();
        }

        /// <inheritdoc/>
        public RequestContext(IRequestContext otherRequestContext)
        {
            if (otherRequestContext == null)
            {
                throw new ArgumentNullException(nameof(otherRequestContext));
            }
            requestContext = new CefSharp.Core.RequestContext(otherRequestContext);
        }

        /// <inheritdoc/>
        public RequestContext(IRequestContext otherRequestContext, IRequestContextHandler requestContextHandler)
        {
            if (otherRequestContext == null)
            {
                throw new ArgumentNullException(nameof(otherRequestContext));
            }

            if (requestContextHandler == null)
            {
                throw new ArgumentNullException(nameof(requestContextHandler));
            }

            requestContext = new CefSharp.Core.RequestContext(otherRequestContext, requestContextHandler);

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Guard the argument: if (otherRequestContext == null) throw or create a fresh RequestContext first.
  2. If you want a brand-new standalone context, use the parameterless RequestContext() constructor instead of the sharing overload.
  3. Wire the source context creation earlier in startup so the reference is never null by the time you share it.
  4. Enable nullable reference types (<Nullable>enable</Nullable>) so the compiler flags the null at the call site.

Example fix

// before
var ctx = new RequestContext(maybeNullParent); // ArgumentNullException

// after
var parent = maybeNullParent ?? new RequestContext();
var ctx = new RequestContext(parent);
Defensive patterns

Strategy: validation

Validate before calling

if (otherRequestContext == null) throw new ArgumentException("parent context required");
var ctx = new RequestContext(otherRequestContext);

Type guard

public static bool IsShareable(IRequestContext c) => c != null && !((c as IDisposable)?.GetType().IsValueType ?? false); // simplify: non-null check
// Practical guard:
public static bool IsValidParent(IRequestContext c) => c != null;

Try / catch

try { return new RequestContext(parent); }
catch (ArgumentNullException ex) when (ex.ParamName == "otherRequestContext")
{ /* create a fresh context instead */ return new RequestContext(); }

Prevention

When it happens

Trigger: Calling new RequestContext(null); passing a variable that was never assigned (e.g. reading from a config that returned null); chaining from another API that can legitimately return null IRequestContext and not null-checking.

Common situations: Building a derived/shared cookie+cache context for an isolated browser and forgetting to instantiate the parent; refactor where the source context is now lazily created and may still be null on first use.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/6fcc72325e15b740. Report an issue: GitHub.