{"record":{"id":"c861beb1684d05fc","repo":"dotnet/wpf","slug":"notimplementedexception-templatecontent","errorCode":null,"errorMessage":"NotImplementedException","messagePattern":"NotImplementedException","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs","lineNumber":1481,"sourceCode":"            {\n                FrugalObjectList<NamespaceDeclaration> namespaces = Frames.InScopeNamespaces;\n                if (namespaces != null)\n                {\n                    for (int idx = 0; idx < namespaces.Count; idx++)\n                    {\n                        if (namespaces[idx].Prefix == prefix)\n                        {\n                            return namespaces[idx].Namespace;\n                        }\n                    }\n                }\n\n                return ((IXamlNamespaceResolver)_services.GetService(typeof(IXamlNamespaceResolver))).GetNamespace(prefix);\n            }\n\n            IEnumerable<NamespaceDeclaration> IXamlNamespaceResolver.GetNamespacePrefixes()\n            {\n                throw new NotImplementedException();\n            }\n\n            #endregion\n\n            #region ITypeDescriptorContext Members\n\n            IContainer ITypeDescriptorContext.Container\n            {\n                get { return null; }\n            }\n\n            object ITypeDescriptorContext.Instance\n            {\n                get { return null; }\n            }\n\n            void ITypeDescriptorContext.OnComponentChanged()\n            {","sourceCodeStart":1463,"sourceCodeEnd":1499,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/TemplateContent.cs#L1463-L1499","documentation":"TemplateContent's internal service provider implements IXamlNamespaceResolver but only supports GetNamespace(prefix); GetNamespacePrefixes() is deliberately unimplemented and always throws NotImplementedException. Any consumer asking for the full prefix enumeration gets this exception by design.","triggerScenarios":"Calling IXamlNamespaceResolver.GetNamespacePrefixes() on the service object obtained from TemplateContent's IServiceProvider (e.g. via GetService(typeof(IXamlNamespaceResolver))).","commonSituations":"Custom markup extensions, value serializers, or schema context code that enumerates all namespace prefixes while template content is being deferred-parsed.","solutions":["Avoid calling GetNamespacePrefixes() inside deferred template content; use GetNamespace(prefix) with a known prefix instead.","Obtain namespace prefixes from the outer/primary XAML parser context rather than the template's service provider.","Guard the call in try/catch on NotImplementedException when the service provider is unknown."],"exampleFix":"// before\nvar prefixes = ((IXamlNamespaceResolver)services.GetService(typeof(IXamlNamespaceResolver))).GetNamespacePrefixes();\n// after\nvar nsResolver = (IXamlNamespaceResolver)services.GetService(typeof(IXamlNamespaceResolver));\nvar ns = nsResolver.GetNamespace(\"x\"); // only GetNamespace is supported","handlingStrategy":"try-catch","validationCode":"var svc = services.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver;\nbool hasService = svc != null; // then prefer GetNamespace(prefix) only","typeGuard":"static bool SupportsPrefixEnumeration(IXamlNamespaceResolver r) { try { r.GetNamespacePrefixes(); return true; } catch (NotImplementedException) { return false; } }","tryCatchPattern":"try { prefixes = nsResolver.GetNamespacePrefixes(); }\ncatch (NotImplementedException) { prefixes = Enumerable.Empty<NamespaceDeclaration>(); }","preventionTips":["Inside deferred template content use GetNamespace(prefix), not prefix enumeration","Get prefix lists from the root XAML parser context","Defensively treat optional IXaml services as partially implemented"],"tags":["wpf","xaml","not-implemented","service-provider"],"backgroundTag":"method-not-implemented","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"}