dotnet/wpf · error · ArgumentException

SR.Format(SR.MarkupExtensionNoContext, GetType().Name…

Error message

SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver))

What it means

StaticExtension.ProvideValue needs an IXamlTypeResolver service from the serviceProvider to convert the type string (possibly with an XML prefix like 'av:Button') into a Type. If the provider returns null for typeof(IXamlTypeResolver), the extension throws MarkupExtensionNoContext, meaning it was evaluated in a context that cannot resolve XAML types.

Solutions

  1. Only call ProvideValue during actual XAML parsing where the parser supplies IXamlTypeResolver.
  2. In tests/custom hosts, supply a service provider implementing IServiceProvider.GetService that returns an IXamlTypeResolver.
  3. If you already have the Type, use MemberType/TypeExtension with a Type instead of resolving via name.

Example fix

// before
var value = new StaticExtension("sys:String.Empty").ProvideValue(new SimpleProvider()); // no IXamlTypeResolver
// after
var value = new StaticExtension("sys:String.Empty").ProvideValue(xamlParserServiceProvider); // parser-supplied provider
Defensive patterns

Strategy: type-guard

Validate before calling

var resolver = serviceProvider?.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
if (resolver == null) throw new InvalidOperationException("ProvideValue requires a service provider with IXamlTypeResolver (XAML parser context)");

Type guard

static bool CanResolveTypes(IServiceProvider sp) => sp?.GetService(typeof(IXamlTypeResolver)) is IXamlTypeResolver;

Try / catch

try { value = ext.ProvideValue(provider); } catch (ArgumentException ex) when (ex.Message.Contains("IXamlTypeResolver")) { /* extension used outside XAML context — resolve type manually */ }

Prevention

When it happens

Trigger: Calling ProvideValue with a service provider that does not offer IXamlTypeResolver — e.g. calling ProvideValue(new ServiceProvider()) manually, or using the extension outside a XAML parser load path.

Common situations: Unit tests invoking ProvideValue with a stub service provider lacking IXamlTypeResolver; runtime code that instantiates markup extensions directly outside XAML parsing; custom host frameworks that don't register the type resolver service.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/StaticExtension.cs:84

                if (dotIndex < 0)
                {
                    throw new ArgumentException(SR.Format(SR.MarkupExtensionBadStatic, _member));
                }

                // Pull out the type substring (this will include any XML prefix, e.g. "av:Button")
                string typeString = _member.Substring(0, dotIndex);
                if (string.IsNullOrEmpty(typeString))
                {
                    throw new ArgumentException(SR.Format(SR.MarkupExtensionBadStatic, _member));
                }

                // Get the IXamlTypeResolver from the service provider

                ArgumentNullException.ThrowIfNull(serviceProvider);

                if (serviceProvider.GetService(typeof(IXamlTypeResolver)) is not IXamlTypeResolver xamlTypeResolver)
                {
                    throw new ArgumentException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver)));
                }

                // Use the type resolver to get a Type instance.
                type = xamlTypeResolver.Resolve(typeString);

                // Get the member name substring.
                fieldString = _member.Substring(dotIndex + 1, _member.Length - dotIndex - 1);
                if (string.IsNullOrEmpty(typeString))
                {
                    throw new ArgumentException(SR.Format(SR.MarkupExtensionBadStatic, _member));
                }
            }

            // Use the built-in parser for enum types.
            if (type.IsEnum)
            {
                return Enum.Parse(type, fieldString);
            }

View on GitHub (pinned to 81131a70a4)