stride3d/stride · error · InvalidOperationException

A template provider with the same name has already been…

Error message

A template provider with the same name has already been registered in this template selector.

What it means

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.

Solutions

  1. Give each template provider a unique Name before registering
  2. Check templateProviderNames (or track registration yourself) and skip duplicates
  3. Unregister or recreate the selector instead of re-registering on repeated initialization

Example fix

// before
selector.RegisterTemplateProvider(provider);
selector.RegisterTemplateProvider(provider); // duplicate name
// after
if (!selector.Providers.Any(p => p.Name == provider.Name))
    selector.RegisterTemplateProvider(provider);
Defensive patterns

Strategy: validation

Validate before calling

if (providers.Any(p => p.Name == newProvider.Name)) return; // skip duplicate

Try / catch

try { selector.RegisterTemplateProvider(p); } catch (InvalidOperationException ex) when (ex.Message.Contains("already been registered")) { /* skip or log */ }

Prevention

When it happens

Trigger: Calling RegisterTemplateProvider twice with providers whose Name properties are equal, or re-registering the same provider instance.

Common situations: 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.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/71f538282d912a03. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/View/TemplateProviderSelector.cs:50

        /// A map of all providers that have already been used for each object, indexed by <see cref="Guid"/>.
        /// </summary>
        private readonly ConditionalWeakTable<object, List<string>> usedProviders = new ConditionalWeakTable<object, List<string>>();

        /// <summary>
        /// A map containing the last container for a given object.
        /// </summary>
        private readonly ConditionalWeakTable<object, WeakReference> lastContainers = new ConditionalWeakTable<object, WeakReference>();

        /// <summary>
        /// Registers the given template into the static <see cref="TemplateProviderSelector"/>.
        /// </summary>
        /// <param name="templateProvider"></param>
        public void RegisterTemplateProvider([NotNull] ITemplateProvider templateProvider)
        {
            if (templateProvider == null) throw new ArgumentNullException(nameof(templateProvider));

            if (templateProviderNames.Contains(templateProvider.Name))
                throw new InvalidOperationException("A template provider with the same name has already been registered in this template selector.");

            InsertTemplateProvider(templateProviders, templateProvider, new List<ITemplateProvider>());
            templateProviderNames.Add(templateProvider.Name);
        }

        /// <summary>
        /// Unregisters the given template into the static <see cref="TemplateProviderSelector"/>.
        /// </summary>
        /// <param name="templateProvider"></param>
        public void UnregisterTemplateProvider([NotNull] ITemplateProvider templateProvider)
        {
            if (templateProviderNames.Remove(templateProvider.Name))
            {
                templateProviders.Remove(templateProvider);
            }
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)

View on GitHub (pinned to 96fad776d2)