dotnet/wpf · error · InvalidOperationException

SR.MissingNameResolver

Error message

SR.MissingNameResolver

What it means

NameReferenceConverter.ConvertFrom throws InvalidOperationException (SR.MissingNameResolver) when the ITypeDescriptorContext's GetService(typeof(IXamlNameResolver)) returns null. Resolving a string name to an object requires an IXamlNameResolver service from the XAML ambient service provider; without it the converter cannot function. This indicates the converter is being used outside a proper XAML name-resolution context.

Solutions

  1. Ensure ConvertFrom is invoked with an ITypeDescriptorContext backed by a real XAML parser/builder context that provides IXamlNameResolver.
  2. In tests or custom hosts, register a mock IXamlNameResolver via the context's GetService.
  3. Fall back to a different conversion path if name resolution services are unavailable.

Example fix

// before
converter.ConvertFrom(new PlainTypeDescriptorContext(), culture, "myName"); // no IXamlNameResolver
// after
var ctx = new XamlTypeDescriptorContext(serviceProviderWithXamlNameResolver);
converter.ConvertFrom(ctx, culture, "myName");
Defensive patterns

Strategy: type-guard

Validate before calling

var nameResolver = context?.GetService(typeof(IXamlNameResolver));
if (nameResolver is null) throw new InvalidOperationException("IXamlNameResolver service unavailable in this context");

Type guard

bool CanConvertNames(ITypeDescriptorContext ctx) => ctx?.GetService(typeof(IXamlNameResolver)) is IXamlNameResolver;

Try / catch

try { var obj = converter.ConvertFrom(context, culture, value); }
catch (InvalidOperationException ex) when (ex.Message == SR.MissingNameResolver) { /* fall back to non-XAML conversion */ }

Prevention

When it happens

Trigger: Calling ConvertFrom with a context whose GetService(typeof(IXamlNameResolver)) returns null — e.g. a plain TypeDescriptor context or a custom service provider that does not supply the XAML name resolver.

Common situations: Invoking the converter outside XAML parsing (designer tools, unit tests with stub contexts); custom IXamlServiceProvider implementations missing service registrations; using the converter during deserialization phases before name scope services are attached.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/NameReferenceConverter.cs:31

    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            ArgumentNullException.ThrowIfNull(context);

            var nameResolver = (IXamlNameResolver)context.GetService(typeof(IXamlNameResolver));
            if (nameResolver is null)
            {
                throw new InvalidOperationException(SR.MissingNameResolver);
            }

            string name = value as string;
            if (string.IsNullOrEmpty(name))
            {
                throw new InvalidOperationException(SR.MustHaveName);
            }

            object obj = nameResolver.Resolve(name);
            if (obj is null)
            {
                string[] names = new string[] { name };
                obj = nameResolver.GetFixupToken(names, true);
            }

            return obj;
        }

View on GitHub (pinned to 81131a70a4)