{"record":{"id":"57e917a6f4788c75","repo":"LykosAI/StabilityMatrix","slug":"service-type-servicetype-is-not-assignable-to-typeof-t","errorCode":null,"errorMessage":"Service type {serviceType} is not assignable to {typeof(T)}","messagePattern":"Service type (.+?) is not assignable to (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/ScopedServiceManager.cs","lineNumber":67,"sourceCode":"        return parentManager.RegisterScoped(type, provider);\n    }\n\n    public IServiceManagerScope<T> CreateScope()\n    {\n        return parentManager.CreateScope();\n    }\n\n    public TService Get<TService>()\n        where TService : T\n    {\n        return (TService)Get(typeof(TService))!;\n    }\n\n    public T Get(Type serviceType)\n    {\n        if (!typeof(T).IsAssignableFrom(serviceType)) // Ensure type compatibility\n        {\n            throw new ArgumentException($\"Service type {serviceType} is not assignable to {typeof(T)}\");\n        }\n\n        // Check if it's a known *scoped* service type from the parent\n        if (parentManager.TryGetScopedProvider(serviceType, out var scopedProvider))\n        {\n            // Create the scoped instance using the factory from the parent\n            var newScopedInstance = scopedProvider(scopedServiceProvider);\n            if (newScopedInstance == null)\n                throw new InvalidOperationException($\"Scoped provider for {serviceType} returned null.\");\n\n            return newScopedInstance;\n        }\n\n        // 3. If not scoped, delegate to the parent manager to resolve Singleton or Transient\n        //    (Parent's Get will throw if the type isn't registered there either)\n        // return parentManager.Get(serviceType);\n\n        // We don't use parent manager for scoped contexts anymore,","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ScopedServiceManager.cs#L49-L85","documentation":"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.","triggerScenarios":"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>.","commonSituations":"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.","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"],"exampleFix":"// before\nvar svc = scopedManager.Get(typeof(UnrelatedService));\n// after\nvar svc = scopedManager.Get(typeof(IModelManager)); // IModelManager : T","handlingStrategy":"type-guard","validationCode":"if (!typeof(IModelManager).IsAssignableFrom(serviceType))\n    throw new ArgumentException($\"{serviceType} must implement the manager's base type\");","typeGuard":"static bool IsCompatible<TBase>(Type serviceType) => typeof(TBase).IsAssignableFrom(serviceType);","tryCatchPattern":"try { var svc = scopedManager.Get(serviceType); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not assignable\"))\n{\n    logger.LogError(ex, \"Requested type {Type} does not implement the manager base type\", serviceType);\n}","preventionTips":["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"],"tags":["di","dependency-injection","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}