dotnet/wpf · error · InvalidOperationException

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

Error message

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

What it means

TypeExtension.ProvideValue throws InvalidOperationException when the supplied IServiceProvider does not provide an IXamlTypeResolver service. Resolving the TypeName string to a Type requires this context service; the library reports the missing context with the extension's own type name in the message.

Solutions

  1. Only call ProvideValue within a real XAML parse, where the parser supplies IXamlTypeResolver.
  2. In tests/mocks, register an IXamlTypeResolver implementation in the service provider.
  3. If the Type is known, set Type directly and ensure the extension returns it early without needing the resolver.
  4. Catch InvalidOperationException and fall back to Type.GetType for simple assembly-qualified names.

Example fix

// before
var resolver = new SimpleServiceProvider(); // no IXamlTypeResolver
var val = new TypeExtension("sys:Int32").ProvideValue(resolver); // throws
// after
var resolver = new SimpleServiceProvider();
resolver.AddService<IXamlTypeResolver>(new MockXamlTypeResolver());
var val = new TypeExtension("sys:Int32").ProvideValue(resolver);
Defensive patterns

Strategy: try-catch

Validate before calling

var resolver = serviceProvider?.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
if (resolver is null) throw new InvalidOperationException("IXamlTypeResolver service unavailable in this context.");

Type guard

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

Try / catch

try { value = ext.ProvideValue(provider); }
catch (InvalidOperationException ex) when (ex.Message.Contains("IXamlTypeResolver")) { value = fallbackType; }

Prevention

When it happens

Trigger: Calling ProvideValue with a service provider whose GetService(typeof(IXamlTypeResolver)) returns null or an object that is not IXamlTypeResolver — e.g. a bare ServiceProvider, a design-time context lacking the resolver, or calling ProvideValue outside XAML parsing.

Common situations: Invoking markup-extension ProvideValue directly in unit tests with a custom/mock IServiceProvider that forgets IXamlTypeResolver; calling it at runtime from code-behind where no XAML schema context exists.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs:71

        {
            // If a type was supplied, no context nor type name are needed
            if (_type is not null)
            {
                return _type;
            }

            // Validate the initialization.
            if (_typeName is null)
            {
                throw new InvalidOperationException(SR.MarkupExtensionTypeName);
            }

            // Get the IXamlTypeResolver from the service provider
            ArgumentNullException.ThrowIfNull(serviceProvider);

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

            // Get the type
            _type = xamlTypeResolver.Resolve(_typeName);
            if (_type is null)
            {
                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionTypeNameBad, _typeName));
            }

            return _type;
        }

        /// <summary>
        /// The typename represented by this markup extension. The name has the format
        /// Prefix:Typename, where Prefix is the xml namespace prefix for this type.
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string TypeName

View on GitHub (pinned to 81131a70a4)