stride3d/stride · error · InvalidOperationException

ObjectId value doesn't match expected size.

Error message

ObjectId value doesn't match expected size.

What it means

Thrown by the ObjectId(byte[] hash) constructor when the input byte array does not have the exact 16-byte length required to represent an ObjectId. The exception is declared in the XML docs for the constructor and fires when callers pass a hash shorter or longer than the fixed size, typically when deserializing or reconstructing an ObjectId from raw or user-supplied bytes.

Solutions

  1. Hash with a 128-bit algorithm (e.g. MD5 or first 16 bytes as Stride intends) before constructing the ObjectId
  2. Verify the byte array length equals ObjectId.HashSize before constructing
  3. If reading stored data, ensure it was written with the full 16-byte ObjectId

Example fix

// before
var id = new ObjectId(sha256Hash); // 32 bytes
// after
var md5 = MD5.HashData(data);
var id = new ObjectId(md5); // exactly 16 bytes
Defensive patterns

Strategy: validation

Validate before calling

if (hash == null || hash.Length != 16) // ObjectId.HashSize
    throw new ArgumentException("ObjectId requires exactly 16 bytes", nameof(hash));
var id = new ObjectId(hash);

Type guard

bool IsValidObjectIdBytes(byte[]? b) => b is { Length: 16 };

Try / catch

try { id = new ObjectId(bytes); }
catch (InvalidOperationException e) when (e.Message.Contains("doesn't match expected size"))
{ /* re-hash with a 128-bit digest or repair stored bytes */ }

Prevention

When it happens

Trigger: new ObjectId(someBytes) where someBytes.Length != 16 — e.g. passing a SHA-256 (32-byte) hash, a truncated hash, or a hex-decoded string of the wrong length.

Common situations: Storing/reading ObjectIds from custom storage where they were written with a different hash size; computing hashes with the wrong algorithm (e.g. SHA-256 instead of the 16-byte digest Stride expects).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/a952b3edc0bc8332. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Storage/ObjectId.cs:57

    public uint Hash3 => hash3;
    public uint Hash4 => hash4;

    /// <summary>
    /// Initializes a new instance of the <see cref="ObjectId"/> struct.
    /// </summary>
    /// <param name="hash">The hash.</param>
    /// <exception cref="System.ArgumentNullException">hash</exception>
    /// <exception cref="System.InvalidOperationException">ObjectId value doesn't match expected size.</exception>
    public ObjectId(byte[] hash)
    {
#if NET7_0_OR_GREATER
        ArgumentNullException.ThrowIfNull(hash);
#else
        if (hash is null) throw new ArgumentNullException(nameof(hash));
#endif // NET7_0_OR_GREATER

        if (hash.Length != HashSize)
            throw new InvalidOperationException("ObjectId value doesn't match expected size.");

        fixed (byte* hashSource = hash)
        {
            var hashSourceCurrent = (uint*)hashSource;
            hash1 = *hashSourceCurrent++;
            hash2 = *hashSourceCurrent++;
            hash3 = *hashSourceCurrent++;
            hash4 = *hashSourceCurrent;
        }
    }

    public ObjectId(uint hash1, uint hash2, uint hash3, uint hash4)
    {
        this.hash1 = hash1;
        this.hash2 = hash2;
        this.hash3 = hash3;
        this.hash4 = hash4;
    }

View on GitHub (pinned to 96fad776d2)