dotnet/wpf · error · ArgumentOutOfRangeException
SeekOrigin value is not valid.
Error message
SeekOrigin value is not valid.
What it means
NetStream.Seek switches on the SeekOrigin parameter (Begin/Current/End); any value outside these three hits the default case and throws ArgumentOutOfRangeException. This library throws it because only the three standard origins are defined.
Solutions
- Pass only SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End
- Validate the origin against Enum.IsDefined(typeof(SeekOrigin), value) before calling Seek
- If a custom origin is needed, compute the absolute offset yourself and use SeekOrigin.Begin
Example fix
// before stream.Seek(delta, (SeekOrigin)myInt); // after var origin = Enum.IsDefined(typeof(SeekOrigin), myInt) ? (SeekOrigin)myInt : SeekOrigin.Begin; stream.Seek(delta, origin);
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(SeekOrigin), origin))
throw new ArgumentException($"Invalid SeekOrigin: {origin}");
stream.Seek(offset, origin); Type guard
static bool IsDefinedSeekOrigin(SeekOrigin o) => o is SeekOrigin.Begin or SeekOrigin.Current or SeekOrigin.End;
Try / catch
try { stream.Seek(offset, origin); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "origin") { origin = SeekOrigin.Begin; stream.Seek(offset, origin); } Prevention
- Never cast ints/bytes to SeekOrigin without Enum.IsDefined validation
- Only reference SeekOrigin's three named members
- Translate custom origin semantics to absolute offsets before seeking
When it happens
Trigger: Calling Seek(long offset, SeekOrigin origin) with an invalid (cast or uninitialized) origin value such as (SeekOrigin)3 or default(SeekOrigin) in non-zero-based code paths.
Common situations: Storing the origin in an int/byte field and casting it back after refactoring; passing a value read from configuration; enum extensions not supported by SeekOrigin (e.g. custom 'Start' origin).
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Cannot set seek pointer to a negative position.
- SeekOrigin value is not valid.
- SR.SeekNegative
- SR.SeekOriginInvalid
- A read or write operation references a location outside the…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f6061064c27668b1.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs:250
temp = offset;
break;
}
case SeekOrigin.Current:
{
temp = _position + offset;
break;
}
case SeekOrigin.End:
{
temp = Length + offset;
break;
}
default:
{
throw new ArgumentOutOfRangeException(nameof(origin), SR.SeekOriginInvalid);
}
}
}
if (temp < 0)
{
throw new ArgumentException(SR.SeekNegative);
}
#if DEBUG
if (System.IO.Packaging.PackWebRequestFactory._traceSwitch.Enabled)
System.Diagnostics.Trace.TraceInformation("NetStream.Seek() pos:{0}", temp);
#endif
_position = temp;
return _position;
}
/// <summary>View on GitHub (pinned to 81131a70a4)