LykosAI/StabilityMatrix · error · ArgumentException

Service type is not assignable to

Error message

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

What it means

ServiceManager<T>.Get(Type) validates that the requested serviceType is assignable to the manager's constraint type T before looking it up. If not, it throws ArgumentException immediately. This is a type-safety guard for the non-generic Get overload.

Solutions

  1. Use the generic Get<TService>() overload instead so the compiler enforces assignability.
  2. Request the service from the ServiceManager whose T the type actually implements.
  3. Fix the type argument: make serviceType implement T or change the requested type.

Example fix

// before
var vm = viewModelManager.Get(typeof(UnrelatedService)); // throws
// after
var svc = serviceManager.Get<ISomeService>();
Defensive patterns

Strategy: type-guard

Validate before calling

if (!typeof(TService).IsAssignableTo(manager.ServiceBaseType)) throw new InvalidOperationException("Wrong manager");

Type guard

static bool ResolvableFrom<TBase>(Type t) => typeof(TBase).IsAssignableFrom(t);

Try / catch

try { var svc = manager.Get(serviceType); }
catch (ArgumentException ex) { logger.Error(ex, "Type {Type} not assignable to manager base"); }

Prevention

When it happens

Trigger: Calling Get(typeof(SomeType)) on a ServiceManager<TBase> where SomeType does not implement/inherit TBase — e.g. passing a concrete ViewModel type to a manager typed on an interface it doesn't implement.

Common situations: Refactoring renamed or moved a service out of an interface hierarchy; passing the wrong manager instance (there are multiple ServiceManager<T> instances, e.g. one for ViewModels, one for services); using typeof(WrongType) in a string/Type-based lookup.

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/6ef2f5ad0ddba18f. Report an issue: GitHub.

Appendix: source

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

    // Internal method for ScopedServiceManager to access providers
    internal bool TryGetScopedProvider(
        Type serviceType,
        [MaybeNullWhen(false)] out Func<IServiceProvider, T> provider
    )
    {
        return scopedProviders.TryGetValue(serviceType, out provider);
    }

    /// <summary>
    /// Get a view model instance from runtime type
    /// </summary>
    [SuppressMessage("ReSharper", "InconsistentlySynchronizedField")]
    public T Get(Type serviceType)
    {
        if (!serviceType.IsAssignableTo(typeof(T)))
        {
            throw new ArgumentException($"Service type {serviceType} is not assignable to {typeof(T)}");
        }

        if (instances.TryGetValue(serviceType, out var instance))
        {
            if (instance is null)
            {
                throw new ArgumentException($"Service of type {serviceType} was registered as null");
            }
            return (T)instance;
        }

        if (providers.TryGetValue(serviceType, out var provider))
        {
            if (provider is null)
            {
                throw new ArgumentException($"Service of type {serviceType} was registered as null");
            }
            var result = provider();

View on GitHub (pinned to af93d6ef57)