LykosAI/StabilityMatrix · error · ArgumentException

Service of type is already registered with a different…

Error message

Service of type {type} is already registered with a different lifetime.

What it means

RegisterScoped<TService>(Func<IServiceProvider,T>) throws ArgumentException when TService already has an instance or provider registered (a 'different lifetime' conflict). Unlike the first check, the second failure (scopedProviders.TryAdd) would mean an existing scoped registration, reported by the 'already registered as Scoped' message.

Solutions

  1. Remove the earlier non-scoped registration for TService
  2. Pick one lifetime per service type and keep it consistent
  3. Order registration so scoped services are only ever registered via RegisterScoped

Example fix

// before
manager.Register<ISession>(() => new Session());
manager.RegisterScoped<ISession>(sp => new Session()); // throws
// after
manager.RegisterScoped<ISession>(sp => new Session()); // single lifetime
Defensive patterns

Strategy: validation

Validate before calling

if (!manager.IsRegistered<ISession>())
    manager.RegisterScoped<ISession>(sp => new Session(sp));

Try / catch

try { manager.RegisterScoped<ISession>(provider); }
catch (ArgumentException ex) when (ex.Message.Contains("different lifetime"))
{
    logger.LogError(ex, "ISession already registered with a non-scoped lifetime");
}

Prevention

When it happens

Trigger: Calling RegisterScoped<TService>(provider) when TService was previously registered as Singleton/Transient via Register (instances/providers contain the key).

Common situations: Registering the same service both as a plain singleton and scoped during setup; migration from an older Register call to RegisterScoped without removing the old line.

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/8f092d7d36c9a106. Report an issue: GitHub.

Appendix: source

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

            }

            providers[type] = providerFunc;
        }
    }

    /// <summary>
    /// Register a new service provider action with Scoped lifetime.
    /// The factory is called once per scope.
    /// </summary>
    public IServiceManager<T> RegisterScoped<TService>(Func<IServiceProvider, TService> provider)
        where TService : T
    {
        var type = typeof(TService);

        lock (providers)
        {
            if (instances.ContainsKey(type) || providers.ContainsKey(type))
                throw new ArgumentException(
                    $"Service of type {type} is already registered with a different lifetime."
                );

            if (!scopedProviders.TryAdd(type, sp => provider(sp))) // Store as base type T
                throw new ArgumentException($"Service of type {type} is already registered as Scoped.");
        }

        return this;
    }

    /// <summary>
    /// Register a new service provider action with Scoped lifetime.
    /// The factory is called once per scope.
    /// </summary>
    public IServiceManager<T> RegisterScoped(Type type, Func<IServiceProvider, T> provider)
    {
        lock (providers)
        {

View on GitHub (pinned to af93d6ef57)