dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException: invalid SeekOrigin value…
Error message
InvalidEnumArgumentException: invalid SeekOrigin value (nameof(origin))
What it means
SharedStream.Seek validates the SeekOrigin parameter and throws InvalidEnumArgumentException when origin is not one of Begin, Current, or End — i.e. an int was cast to SeekOrigin outside its defined range.
Solutions
- Pass only SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End — never a raw cast int.
- Validate any externally supplied origin value before casting: if (!Enum.IsDefined(typeof(SeekOrigin), v)) ...
- Fix the source of the bad value (saved state, interop marshaling) rather than the call site.
- Default to SeekOrigin.Begin when the origin is unknown.
Example fix
// before
stream.Seek(offset, (SeekOrigin)savedOrigin);
// after
if (!Enum.IsDefined(typeof(SeekOrigin), savedOrigin)) throw new InvalidDataException($"bad origin {savedOrigin}");
stream.Seek(offset, (SeekOrigin)savedOrigin); Defensive patterns
Strategy: type-guard
Validate before calling
// Validate SeekOrigin before calling Seek bool IsValidSeekOrigin(SeekOrigin origin) => origin is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End; // for raw ints: Enum.IsDefined(typeof(SeekOrigin), value)
Type guard
static bool IsDefinedSeekOrigin(int v) => v is >= 0 and <= 2; // Begin, Current, End
Try / catch
try
{
stream.Seek(offset, origin);
}
catch (InvalidEnumArgumentException ex)
{
throw new InvalidDataException($"Origin must be Begin/Current/End, got {origin}", ex);
} Prevention
- Never cast raw ints to SeekOrigin without Enum.IsDefined
- Initialize SeekOrigin variables explicitly (default to SeekOrigin.Begin)
- Check interop marshaling of origin values
- Use the enum type in your own APIs to get compile-time safety
When it happens
Trigger: Calling sharedStream.Seek(offset, (SeekOrigin)someInt) where someInt is not 0, 1, or 2 — typically an uninitialized variable, bad interop value, or numeric constant mismatch.
Common situations: Interop/P-Invoke code passing raw longs into SeekOrigin casts; deserialization restoring a saved stream position with a corrupt origin field; off-by-one constants (3+).
Understand the failure class
Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.
Related errors
- SR.FileModeInvalid
- SR.ParserWriterUnknownOrigin
- ArgumentOutOfRangeException(nameof(offset))
- ArgumentOutOfRangeException: offset is outside the valid…
- ArgumentOutOfRangeException (Parameter 'origin')
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/da759384a57084a8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/SharedStream.cs:234
public override long Seek(long offset, SeekOrigin origin)
{
long newPosition;
switch (origin)
{
case SeekOrigin.Begin:
newPosition = offset;
break;
case SeekOrigin.Current:
newPosition = _position + offset;
break;
case SeekOrigin.End:
newPosition = _length + offset;
break;
default:
throw new InvalidEnumArgumentException(nameof(origin), (int)origin, typeof(SeekOrigin));
}
if (newPosition < 0 || newPosition >= _length)
{
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
}
CheckDisposed();
_position = newPosition;
return _position;
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}View on GitHub (pinned to 81131a70a4)