dotnet/wpf · error · ArgumentOutOfRangeException

Specified argument was out of range of valid values…

Error message

Specified argument was out of range of valid values. (Parameter 'value')

What it means

PageOrientationSetting.Value accepts only values within PrintSchema.PageOrientationEnumMin..Max; assigning anything outside that range throws ArgumentOutOfRangeException('value'). The orientation is written to the ticket's OptionName property.

Solutions

  1. Assign only valid PageOrientation enum members (Portrait, Landscape, ReversePortrait, ReverseLandscape).
  2. Use Enum.IsDefined before casting raw ints.
  3. Map unknown/unsupported orientations to PageOrientation.Portrait.
  4. Catch ArgumentOutOfRangeException and use the queue's default orientation.

Example fix

// before
printTicket.PageOrientation = (PageOrientation)userValue; // e.g. 99
// after
printTicket.PageOrientation = Enum.IsDefined(typeof(PageOrientation), userValue)
    ? (PageOrientation)userValue : PageOrientation.Portrait;
Defensive patterns

Strategy: validation

Validate before calling

if (Enum.IsDefined(typeof(PageOrientation), rawValue))
    printTicket.PageOrientation = (PageOrientation)rawValue;

Type guard

bool IsValidPageOrientation(object v) => v is int i && i >= PrintSchema.PageOrientationEnumMin && i <= PrintSchema.PageOrientationEnumMax;

Try / catch

try { printTicket.PageOrientation = value; }
catch (ArgumentOutOfRangeException) { printTicket.PageOrientation = PageOrientation.Portrait; }

Prevention

When it happens

Trigger: Setting PrintTicket.PageOrientation to an out-of-range value (undefined enum member or invalid int cast).

Common situations: Casting ints from config or device-specific settings into PageOrientation; legacy code using removed enum values; parsing orientation strings with int.Parse without validation.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/3630b4457841e0d9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/PrintConfig/PageOrientation.cs:252

        /// </summary>
        /// <remarks>
        /// If the setting is not specified yet, getter will return 0.
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException">
        /// The value to set is not one of the standard <see cref="PageOrientation"/>.
        /// </exception>
        public PageOrientation Value
        {
            get
            {
                return (PageOrientation)this[PrintSchemaTags.Framework.OptionNameProperty];
            }
            set
            {
                if (value < PrintSchema.PageOrientationEnumMin ||
                    value > PrintSchema.PageOrientationEnumMax)
                {
                    throw new ArgumentOutOfRangeException(nameof(value));
                }

                this[PrintSchemaTags.Framework.OptionNameProperty] = (int)value;
            }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Converts the page orientation setting to human-readable string.
        /// </summary>
        /// <returns>A string that represents this page orientation setting.</returns>
        public override string ToString()
        {
            return Value.ToString();
        }

View on GitHub (pinned to 81131a70a4)