LykosAI/StabilityMatrix · error · ArgumentException

Service of type is already registered for

Error message

Service of type {typeof(TService)} is already registered for {typeof(T)}

What it means

ServiceManager<T>.Register<TService>(instance) throws ArgumentException when an instance or provider for TService is already registered, enforcing one registration per service type. The lock guarantees thread-safe duplicate detection.

Solutions

  1. Guard with TryGet/ContainsKey before registering
  2. Replace the existing registration instead of re-registering if replacement is intended
  3. Ensure initialization code runs only once (idempotent setup)

Example fix

// before
manager.Register<IModelManager>(new LocalModelManager()); // may run twice
// after
if (!manager.IsRegistered<IModelManager>())
    manager.Register<IModelManager>(new LocalModelManager());
Defensive patterns

Strategy: validation

Validate before calling

if (manager.IsRegistered<TService>()) return manager.Get<TService>();
manager.Register<TService>(new TService());

Try / catch

try { manager.Register<IFoo>(foo); }
catch (ArgumentException ex) when (ex.Message.Contains("already registered"))
{
    // registration already exists; safe to ignore or fetch existing
    existing = manager.Get<IFoo>();
}

Prevention

When it happens

Trigger: Calling Register<IFoo>(new Foo()) twice, or registering IFoo via Register after a Register<IFoo>(Func<T>) provider already exists for the same key in instances/providers.

Common situations: Double app initialization (e.g. DI setup executed on both design-time and runtime paths); registry code like LocalProviderModelManagerRegistry registering the same model manager type repeatedly across reloads.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/0fa585f78091d6e5. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Services/ServiceManager.cs:34

    private readonly Dictionary<Type, T> instances = new();

    // Holds scoped providers (factories)
    private readonly ConcurrentDictionary<Type, Func<IServiceProvider, T>> scopedProviders = new();

    /// <summary>
    /// Register a new dialog view model (singleton instance)
    /// </summary>
    public IServiceManager<T> Register<TService>(TService instance)
        where TService : T
    {
        if (instance is null)
            throw new ArgumentNullException(nameof(instance));

        lock (instances)
        {
            if (instances.ContainsKey(typeof(TService)) || providers.ContainsKey(typeof(TService)))
            {
                throw new ArgumentException(
                    $"Service of type {typeof(TService)} is already registered for {typeof(T)}"
                );
            }

            instances[instance.GetType()] = instance;
        }

        return this;
    }

    /// <summary>
    /// Register a new dialog view model provider action (called on each dialog creation)
    /// </summary>
    public IServiceManager<T> Register<TService>(Func<TService> provider)
        where TService : T
    {
        lock (providers)
        {

View on GitHub (pinned to af93d6ef57)