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
- Request a type that implements/inherits T
- Use the strongly-typed Get<TService>() overload instead of Get(Type)
- 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
- Prefer the generic Get<TService>() overload for compile-time safety
- Keep service interfaces inheriting the manager's base type T
- Never pass implementation types as lookup keys
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
- Scoped provider for returned null.
- Service of type is already registered for
- Service of type is already registered for
- Service type is not assignable to
- Convert Target type must be assignable to
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)