LykosAI/StabilityMatrix · error · ArgumentException

Service type is not assignable to

Error message

Service type {serviceType} is not assignable to {typeof(T)}

What it means

ScopedServiceManager<T>.Get(Type) throws ArgumentException when the requested serviceType does not implement/inherit the manager's base type T. The guard exists to fail fast on type-incompatible lookups before consulting the parent provider.

Solutions

  1. Request a type that implements/inherits T
  2. Use the strongly-typed Get<TService>() overload instead of Get(Type)
  3. Move the lookup to the manager whose T matches the requested type

Example fix

// before
var svc = scopedManager.Get(typeof(UnrelatedService));
// after
var svc = scopedManager.Get(typeof(IModelManager)); // IModelManager : T
Defensive patterns

Strategy: type-guard

Validate before calling

if (!typeof(IModelManager).IsAssignableFrom(serviceType))
    throw new ArgumentException($"{serviceType} must implement the manager's base type");

Type guard

static bool IsCompatible<TBase>(Type serviceType) => typeof(TBase).IsAssignableFrom(serviceType);

Try / catch

try { var svc = scopedManager.Get(serviceType); }
catch (ArgumentException ex) when (ex.Message.Contains("not assignable"))
{
    logger.LogError(ex, "Requested type {Type} does not implement the manager base type", serviceType);
}

Prevention

When it happens

Trigger: Calling scopedManager.Get(someType) where someType is not assignable to T — e.g. passing a concrete implementation type or an unrelated interface to a manager typed as IServiceManager<IBaseService>.

Common situations: Mixing up generic parameter and lookup type; refactoring a service to no longer implement T while call sites still query through the old manager; passing typeof(Implementation) instead of the interface it was registered under.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at StabilityMatrix.Avalonia/Services/ScopedServiceManager.cs:67

        return parentManager.RegisterScoped(type, provider);
    }

    public IServiceManagerScope<T> CreateScope()
    {
        return parentManager.CreateScope();
    }

    public TService Get<TService>()
        where TService : T
    {
        return (TService)Get(typeof(TService))!;
    }

    public T Get(Type serviceType)
    {
        if (!typeof(T).IsAssignableFrom(serviceType)) // Ensure type compatibility
        {
            throw new ArgumentException($"Service type {serviceType} is not assignable to {typeof(T)}");
        }

        // Check if it's a known *scoped* service type from the parent
        if (parentManager.TryGetScopedProvider(serviceType, out var scopedProvider))
        {
            // Create the scoped instance using the factory from the parent
            var newScopedInstance = scopedProvider(scopedServiceProvider);
            if (newScopedInstance == null)
                throw new InvalidOperationException($"Scoped provider for {serviceType} returned null.");

            return newScopedInstance;
        }

        // 3. If not scoped, delegate to the parent manager to resolve Singleton or Transient
        //    (Parent's Get will throw if the type isn't registered there either)
        // return parentManager.Get(serviceType);

        // We don't use parent manager for scoped contexts anymore,

View on GitHub (pinned to af93d6ef57)