dotnet/wpf · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException(right)

Error message

ArgumentOutOfRangeException(right)

What it means

The ContentGrant constructor validates the right parameter against the supported ContentRight values (View, Edit, Print, Forward, Reply, ReplyAll, Sign, DocumentEdit, Export, and Owner per the guard chain) and throws ArgumentOutOfRangeException(nameof(right)) when the value is not one of them. This guards against undefined enum values leaking into the rights-management grant model.

Solutions

  1. Pass only defined ContentRight members from the supported set
  2. Validate the value with Enum.IsDefined (and membership in the supported subset) before constructing ContentGrant
  3. Fix deserialization code to map/whitelist int values to valid ContentRights
  4. If a newer right is needed, use a library/runtime version that defines it

Example fix

// before
var grant = new ContentGrant(user, (ContentRight)rawInt); // throws for undefined values
// after
if (!Enum.IsDefined(typeof(ContentRight), rawInt))
    throw new ArgumentException($"Unsupported right: {rawInt}");
var grant = new ContentGrant(user, (ContentRight)rawInt);
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<ContentRight> Supported = new()
    { ContentRight.View, ContentRight.Edit, ContentRight.Print, ContentRight.Forward,
      ContentRight.Reply, ContentRight.ReplyAll, ContentRight.Sign,
      ContentRight.DocumentEdit, ContentRight.Export };
bool IsValidRight(ContentRight r) => Supported.Contains(r);

Type guard

bool IsDefinedContentRight(object value) =>
    value is ContentRight r && Enum.IsDefined(typeof(ContentRight), r);

Try / catch

try { var grant = new ContentGrant(user, right); }
catch (ArgumentOutOfRangeException)
{ /* map to a supported right or reject input */ }

Prevention

When it happens

Trigger: Constructing new ContentGrant(user, right) with an undefined or uncast ContentRight value, e.g. (ContentRight)999 or a right obtained from an unvalidated external source.

Common situations: Deserializing rights from config/database as int and casting to ContentRight; version mismatch where a newer enum member is passed to an older library; copy-paste of an incorrect enum type with coincident numeric values.

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/c36ebae40236ce2f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Security/RightsManagement/Grant.cs:44

            // Add validation here 

            ArgumentNullException.ThrowIfNull(user);

            if ((right != ContentRight.View) && 
                (right != ContentRight.Edit) && 
                (right != ContentRight.Print) && 
                (right != ContentRight.Extract) && 
                (right != ContentRight.ObjectModel) && 
                (right != ContentRight.Owner) && 
                (right != ContentRight.ViewRightsData) && 
                (right != ContentRight.Forward) && 
                (right != ContentRight.Reply) &&
                (right != ContentRight.ReplyAll) &&
                (right != ContentRight.Sign) &&
                (right != ContentRight.DocumentEdit)  &&
                (right != ContentRight.Export))
            {
                throw new ArgumentOutOfRangeException(nameof(right));                
            }

            ArgumentOutOfRangeException.ThrowIfGreaterThan(validFrom, validUntil);

            _user = user;
            _right = right;

            _validFrom = validFrom;
            _validUntil = validUntil;
        }



        /// <summary>
        /// Read only User propery.
        /// </summary>
        public ContentUser User
        {

View on GitHub (pinned to 81131a70a4)