dotnet/wpf · error · ArgumentException

SR.AssemblyIdNegative

Error message

SR.AssemblyIdNegative

What it means

GetKnownBamlAssembly maps negated BAML assembly ids to known assemblies (ids are stored negative). A zero or positive assemblyId is invalid input, so an ArgumentException with SR.AssemblyIdNegative is thrown. The contract is that only negative encoded ids are accepted; the method negates the value back internally.

Solutions

  1. Pass the id exactly as encoded in BAML (negative); the method negates it internally.
  2. Verify the BAML stream is not corrupted — regenerate it by recompiling the XAML.
  3. If decoding manually, negate the value before calling: GetKnownBamlAssembly((short)-rawId).
  4. Validate the id is < 0 before calling and skip/log otherwise.

Example fix

// before
var asm = schemaContext.GetKnownBamlAssembly((short)3); // positive id throws
// after
var asm = schemaContext.GetKnownBamlAssembly((short)-3); // negated BAML-encoded id
Defensive patterns

Strategy: validation

Validate before calling

if (assemblyId >= 0) throw new ArgumentException($"BAML assembly ids are stored negated; got {assemblyId}.");

Type guard

bool IsEncodedBamlAssemblyId(short id) => id < 0;

Try / catch

try { asm = ctx.GetKnownBamlAssembly(id); }
catch (ArgumentException) { /* log invalid encoded id; treat stream as corrupt */ }

Prevention

When it happens

Trigger: Calling GetKnownBamlAssembly with an assemblyId that is 0 or positive, i.e. an id that was not the negated encoding produced by the BAML schema context.

Common situations: Off-by-sign bugs when reading BAML records manually; corrupted or hand-crafted BAML streams with a wrong assembly id; tooling that decodes BAML and forgets to negate the id.

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.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedBamlSchemaContext.cs:83

            {
                case -1:
                    result = "Name";
                    break;
                case -2:
                    result = "Uid";
                    break;
                default:
                    result = null;
                    break;
            }
            return result;
        }

        internal Baml6Assembly GetKnownBamlAssembly(Int16 assemblyId)
        {
            if (assemblyId > 0)
            {
                throw new ArgumentException(SR.AssemblyIdNegative);
            }
            assemblyId = (short)-assemblyId;

            Baml6Assembly assembly = _knownBamlAssemblies[assemblyId];
            if (assembly == null)
            {
                assembly = CreateKnownBamlAssembly(assemblyId);
                _knownBamlAssemblies[assemblyId] = assembly;
            }
            return assembly;
        }

        internal Baml6Assembly CreateKnownBamlAssembly(Int16 assemblyId)
        {
            Baml6Assembly assembly;

            switch (assemblyId)
            {

View on GitHub (pinned to 81131a70a4)