dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(clrNamespace))

Error message

ArgumentNullException(nameof(clrNamespace))

What it means

The XmlnsDefinitionAttribute constructor throws ArgumentNullException when the clrNamespace parameter is null. The CLR namespace is the target of the XAML namespace mapping; without it the attribute cannot tell the XAML processor where to look for types.

Solutions

  1. Pass a non-null CLR namespace string (e.g. "MyApp.Controls") as the second constructor argument.
  2. Verify the namespace actually exists in the assembly so the mapping resolves types.
  3. If the value is computed, add a null check/guard before constructing the attribute.

Example fix

// before
[assembly: XmlnsDefinition("http://schemas.myapp.com/controls", null)]
// after
[assembly: XmlnsDefinition("http://schemas.myapp.com/controls", "MyApp.Controls")]
Defensive patterns

Strategy: validation

Validate before calling

if (clrNamespace is null) throw new ArgumentException("clrNamespace must be non-null", nameof(clrNamespace));
var attr = new XmlnsDefinitionAttribute(xmlNamespace, clrNamespace);

Type guard

bool IsValidClrNamespace(string? clr) => !string.IsNullOrEmpty(clr);

Try / catch

try { var attr = new XmlnsDefinitionAttribute(ns, clr); }
catch (ArgumentNullException ex) { /* ex.ParamName == "clrNamespace": fix or skip mapping */ }

Prevention

When it happens

Trigger: Calling new XmlnsDefinitionAttribute(someXmlNamespace, null) — an assembly-level mapping whose second argument is null.

Common situations: Typo or refactoring removed the CLR namespace string in AssemblyInfo.cs; generated code passing an uninitialized namespace variable.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/XmlnsDefinitionAttribute.cs:49

    ///   /Page
    /// </summary>
    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
    [TypeForwardedFrom("WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
    public sealed class XmlnsDefinitionAttribute : Attribute
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="xmlNamespace">
        /// XmlNamespace used by Markup file
        /// </param>
        /// <param name="clrNamespace">
        /// Clr namespace which contains known types that are used by Markup File.
        /// </param>
        public XmlnsDefinitionAttribute(string xmlNamespace, string clrNamespace)
        {
            XmlNamespace = xmlNamespace ?? throw new ArgumentNullException(nameof(xmlNamespace));
            ClrNamespace = clrNamespace ?? throw new ArgumentNullException(nameof(clrNamespace));
        }

        /// <summary>
        /// XmlNamespace which can be used in Markup file.
        /// such as XmlNamespace is set to
        /// "http://schemas.fabrikam.com/mynamespace".
        ///
        /// The markup file can have definition like
        /// xmlns:myns="http://schemas.fabrikam.com/mynamespace"
        /// </summary>
        public string XmlNamespace { get; }

        /// <summary>
        /// ClrNamespace which map to XmlNamespace.
        /// This ClrNamespace should contain some types which are used
        /// by Xaml markup file.
        /// </summary>
        public string ClrNamespace { get; }

View on GitHub (pinned to 81131a70a4)