cefsharp/CefSharp · error · Exception

Unable to load byte[] with length 0.

Error message

Unable to load byte[] with length 0.

What it means

Thrown by the ByteArrayResourceHandler constructor when the data array is non-null but has length 0. A zero-length body is treated as invalid input because a stream with no content produces a meaningless/empty response; the handler rejects it rather than letting CEF serve an empty body. Note this uses a base Exception (not ArgumentException).

Source

Thrown at CefSharp/Internals/ByteArrayResourceHandler.cs:48

        /// Initializes a new instance of the <see cref="ByteArrayResourceHandler"/> class.
        /// </summary>
        /// <param name="mimeType">mimeType</param>
        /// <param name="data">byte array</param>
        public ByteArrayResourceHandler(string mimeType, byte[] data)
        {
            if (string.IsNullOrEmpty(mimeType))
            {
                throw new ArgumentNullException("mimeType", "Please provide a valid mimeType");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data", "Please provide a valid array");
            }

            if (data.Length == 0)
            {
                throw new Exception("Unable to load byte[] with length 0.");
            }

            MimeType = mimeType;
            Data = data;
        }

        bool IResourceHandler.ProcessRequest(IRequest request, ICallback callback)
        {
            //Should never be called
            throw new NotImplementedException("This method should never be called");
        }

        void IResourceHandler.GetResponseHeaders(IResponse response, out long responseLength, out string redirectUrl)
        {
            //Should never be called
            throw new NotImplementedException("This method should never be called");
        }

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Validate data.Length > 0 before constructing the handler and supply real content or fall back to a default body.
  2. If empty content is legitimately possible, return null from GetResourceHandler to let the request proceed with default handling.
  3. Log/inspect the upstream content source (template, serializer) to find why it produced zero bytes.

Example fix

// before
var bytes = Encoding.UTF8.GetBytes(RenderTemplate(model));
return new ByteArrayResourceHandler("text/html", bytes);

// after
var bytes = Encoding.UTF8.GetBytes(RenderTemplate(model) ?? string.Empty);
if (bytes.Length == 0) return null;
return new ByteArrayResourceHandler("text/html", bytes);
Defensive patterns

Strategy: validation

Validate before calling

if (data == null || data.Length == 0)
{
    return null; // let default handling proceed, or log upstream
}
return new ByteArrayResourceHandler(mimeType, data);

Type guard

static bool HasByteArrayData(byte[] data) => data != null && data.Length > 0;

Prevention

When it happens

Trigger: Constructing new ByteArrayResourceHandler("text/html", new byte[0]) or passing Encoding.UTF8.GetBytes("") (empty string), an emptied MemoryStream.ToArray(), or a collection that yielded no bytes.

Common situations: Rendering a template that produced an empty string; serializing an object whose JSON output was unexpectedly empty; reading a file/resource that was truncated to zero bytes; a buffer that was allocated but never written.

Related errors


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