{"record":{"id":"71f538282d912a03","repo":"stride3d/stride","slug":"a-template-provider-with-the-same-name-has-already-been","errorCode":null,"errorMessage":"A template provider with the same name has already been registered in this template selector.","messagePattern":"A template provider with the same name has already been registered in this template selector\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/View/TemplateProviderSelector.cs","lineNumber":50,"sourceCode":"        /// A map of all providers that have already been used for each object, indexed by <see cref=\"Guid\"/>.\n        /// </summary>\n        private readonly ConditionalWeakTable<object, List<string>> usedProviders = new ConditionalWeakTable<object, List<string>>();\n\n        /// <summary>\n        /// A map containing the last container for a given object.\n        /// </summary>\n        private readonly ConditionalWeakTable<object, WeakReference> lastContainers = new ConditionalWeakTable<object, WeakReference>();\n\n        /// <summary>\n        /// Registers the given template into the static <see cref=\"TemplateProviderSelector\"/>.\n        /// </summary>\n        /// <param name=\"templateProvider\"></param>\n        public void RegisterTemplateProvider([NotNull] ITemplateProvider templateProvider)\n        {\n            if (templateProvider == null) throw new ArgumentNullException(nameof(templateProvider));\n\n            if (templateProviderNames.Contains(templateProvider.Name))\n                throw new InvalidOperationException(\"A template provider with the same name has already been registered in this template selector.\");\n\n            InsertTemplateProvider(templateProviders, templateProvider, new List<ITemplateProvider>());\n            templateProviderNames.Add(templateProvider.Name);\n        }\n\n        /// <summary>\n        /// Unregisters the given template into the static <see cref=\"TemplateProviderSelector\"/>.\n        /// </summary>\n        /// <param name=\"templateProvider\"></param>\n        public void UnregisterTemplateProvider([NotNull] ITemplateProvider templateProvider)\n        {\n            if (templateProviderNames.Remove(templateProvider.Name))\n            {\n                templateProviders.Remove(templateProvider);\n            }\n        }\n\n        public override DataTemplate SelectTemplate(object item, DependencyObject container)","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/View/TemplateProviderSelector.cs#L32-L68","documentation":"TemplateProviderSelector keeps a registry of template providers keyed by their Name. RegisterTemplateProvider throws InvalidOperationException when a provider whose Name is already present is registered, because duplicate names would make template resolution ambiguous.","triggerScenarios":"Calling RegisterTemplateProvider twice with providers whose Name properties are equal, or re-registering the same provider instance.","commonSituations":"XAML or startup code executed more than once (e.g. re-created app resources) registering the same providers again; two providers accidentally given the same name; missing a prior Unregister/cleanup.","solutions":["Give each template provider a unique Name before registering","Check templateProviderNames (or track registration yourself) and skip duplicates","Unregister or recreate the selector instead of re-registering on repeated initialization"],"exampleFix":"// before\nselector.RegisterTemplateProvider(provider);\nselector.RegisterTemplateProvider(provider); // duplicate name\n// after\nif (!selector.Providers.Any(p => p.Name == provider.Name))\n    selector.RegisterTemplateProvider(provider);","handlingStrategy":"validation","validationCode":"if (providers.Any(p => p.Name == newProvider.Name)) return; // skip duplicate","typeGuard":null,"tryCatchPattern":"try { selector.RegisterTemplateProvider(p); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"already been registered\")) { /* skip or log */ }","preventionTips":["Assign unique names to providers","Make registration idempotent","Track registrations at app level"],"tags":["wpf","registration","duplicate-key"],"backgroundTag":"file-already-exists","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}