{"record":{"id":"e5ab04cf30d71a97","repo":"dotnet/wpf","slug":"sr-missingnameprovider","errorCode":null,"errorMessage":"SR.MissingNameProvider","messagePattern":"SR\\.MissingNameProvider","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/NameReferenceConverter.cs","lineNumber":72,"sourceCode":"                return false;\n            }\n\n            if (destinationType == typeof(string))\n            {\n                return true;\n            }\n\n            return base.CanConvertTo(context, destinationType);\n        }\n\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            ArgumentNullException.ThrowIfNull(context);\n\n            var nameProvider = (IXamlNameProvider)context.GetService(typeof(IXamlNameProvider));\n            if (nameProvider is null)\n            {\n                throw new InvalidOperationException(SR.MissingNameProvider);\n            }\n\n            return nameProvider.GetName(value);\n        }\n    }\n}\n","sourceCodeStart":54,"sourceCodeEnd":79,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/NameReferenceConverter.cs#L54-L79","documentation":"NameReferenceConverter.ConvertTo throws InvalidOperationException (SR.MissingNameProvider) when the context's GetService(typeof(IXamlNameProvider)) returns null. Serializing an object back to its XAML name requires an IXamlNameProvider service; without it the converter cannot produce a name string. This mirrors the ConvertFrom MissingNameResolver error on the serialization side.","triggerScenarios":"Calling ConvertTo with an ITypeDescriptorContext whose GetService(typeof(IXamlNameProvider)) returns null — e.g. serializing outside a XAML save pipeline or with a stub context.","commonSituations":"Custom XAML serializers or designer export paths that forget to supply IXamlNameProvider; unit tests exercising ConvertTo with minimal contexts; serialization invoked before the name scope has been established.","solutions":["Invoke ConvertTo within a XAML serialization pipeline (XamlObjectWriter/XamlXmlWriter ecosystem) that provides IXamlNameProvider.","In custom hosts, implement and register IXamlNameProvider on the service provider backing the context.","Check for the service's availability before calling ConvertTo and choose a fallback serialization strategy otherwise."],"exampleFix":"// before\nconverter.ConvertTo(new PlainContext(), culture, obj, typeof(string)); // no IXamlNameProvider\n// after\nvar ctx = new XamlSerializationContext(serviceProviderWithNameProvider);\nvar name = (string)converter.ConvertTo(ctx, culture, obj, typeof(string));","handlingStrategy":"type-guard","validationCode":"var nameProvider = context?.GetService(typeof(IXamlNameProvider));\nif (nameProvider is null) throw new InvalidOperationException(\"IXamlNameProvider service unavailable; cannot serialize name\");","typeGuard":"bool CanConvertToName(ITypeDescriptorContext ctx) => ctx?.GetService(typeof(IXamlNameProvider)) is IXamlNameProvider;","tryCatchPattern":"try { var name = (string)converter.ConvertTo(context, culture, obj, typeof(string)); }\ncatch (InvalidOperationException ex) when (ex.Message == SR.MissingNameProvider) { /* use fallback serialization */ }","preventionTips":["Run ConvertTo only inside a XAML save pipeline that establishes IXamlNameProvider.","Register IXamlNameProvider in custom serializer hosts and designers.","Verify service availability in an integration test for your serialization path."],"tags":["missing-service","xaml","serialization","service-provider"],"backgroundTag":"missing-dependency","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}