{"record":{"id":"c291f94466539e29","repo":"dotnet/wpf","slug":"sr-format-sr-markupextensionnocontext-gettype-name-nameof-c291f9","errorCode":null,"errorMessage":"SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver))","messagePattern":"SR\\.Format\\(SR\\.MarkupExtensionNoContext, GetType\\(\\)\\.Name, nameof\\(IXamlTypeResolver\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs","lineNumber":71,"sourceCode":"        {\n            // If a type was supplied, no context nor type name are needed\n            if (_type is not null)\n            {\n                return _type;\n            }\n\n            // Validate the initialization.\n            if (_typeName is null)\n            {\n                throw new InvalidOperationException(SR.MarkupExtensionTypeName);\n            }\n\n            // Get the IXamlTypeResolver from the service provider\n            ArgumentNullException.ThrowIfNull(serviceProvider);\n\n            if (serviceProvider.GetService(typeof(IXamlTypeResolver)) is not IXamlTypeResolver xamlTypeResolver)\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver)));\n            }\n\n            // Get the type\n            _type = xamlTypeResolver.Resolve(_typeName);\n            if (_type is null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionTypeNameBad, _typeName));\n            }\n\n            return _type;\n        }\n\n        /// <summary>\n        /// The typename represented by this markup extension. The name has the format\n        /// Prefix:Typename, where Prefix is the xml namespace prefix for this type.\n        /// </summary>\n        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]\n        public string TypeName","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs#L53-L89","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only call ProvideValue within a real XAML parse, where the parser supplies IXamlTypeResolver.","In tests/mocks, register an IXamlTypeResolver implementation in the service provider.","If the Type is known, set Type directly and ensure the extension returns it early without needing the resolver.","Catch InvalidOperationException and fall back to Type.GetType for simple assembly-qualified names."],"exampleFix":"// before\nvar resolver = new SimpleServiceProvider(); // no IXamlTypeResolver\nvar val = new TypeExtension(\"sys:Int32\").ProvideValue(resolver); // throws\n// after\nvar resolver = new SimpleServiceProvider();\nresolver.AddService<IXamlTypeResolver>(new MockXamlTypeResolver());\nvar val = new TypeExtension(\"sys:Int32\").ProvideValue(resolver);","handlingStrategy":"try-catch","validationCode":"var resolver = serviceProvider?.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;\nif (resolver is null) throw new InvalidOperationException(\"IXamlTypeResolver service unavailable in this context.\");","typeGuard":"bool HasTypeResolver(IServiceProvider sp) => sp?.GetService(typeof(IXamlTypeResolver)) is IXamlTypeResolver;","tryCatchPattern":"try { value = ext.ProvideValue(provider); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IXamlTypeResolver\")) { value = fallbackType; }","preventionTips":["Invoke markup-extension ProvideValue only during real XAML parsing or with a properly seeded service provider.","In unit tests, register a mock IXamlTypeResolver in the service provider.","Prefer setting Type directly when no resolver context exists."],"tags":["xaml","wpf","service-provider","markup-extension"],"backgroundTag":"missing-dependency","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}