{"record":{"id":"6ef2f5ad0ddba18f","repo":"LykosAI/StabilityMatrix","slug":"service-type-servicetype-is-not-assignable-to-typeof-t-6ef2f5","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/ServiceManager.cs","lineNumber":175,"sourceCode":"\n    // Internal method for ScopedServiceManager to access providers\n    internal bool TryGetScopedProvider(\n        Type serviceType,\n        [MaybeNullWhen(false)] out Func<IServiceProvider, T> provider\n    )\n    {\n        return scopedProviders.TryGetValue(serviceType, out provider);\n    }\n\n    /// <summary>\n    /// Get a view model instance from runtime type\n    /// </summary>\n    [SuppressMessage(\"ReSharper\", \"InconsistentlySynchronizedField\")]\n    public T Get(Type serviceType)\n    {\n        if (!serviceType.IsAssignableTo(typeof(T)))\n        {\n            throw new ArgumentException($\"Service type {serviceType} is not assignable to {typeof(T)}\");\n        }\n\n        if (instances.TryGetValue(serviceType, out var instance))\n        {\n            if (instance is null)\n            {\n                throw new ArgumentException($\"Service of type {serviceType} was registered as null\");\n            }\n            return (T)instance;\n        }\n\n        if (providers.TryGetValue(serviceType, out var provider))\n        {\n            if (provider is null)\n            {\n                throw new ArgumentException($\"Service of type {serviceType} was registered as null\");\n            }\n            var result = provider();","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ServiceManager.cs#L157-L193","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the generic Get<TService>() overload instead so the compiler enforces assignability.","Request the service from the ServiceManager whose T the type actually implements.","Fix the type argument: make serviceType implement T or change the requested type."],"exampleFix":"// before\nvar vm = viewModelManager.Get(typeof(UnrelatedService)); // throws\n// after\nvar svc = serviceManager.Get<ISomeService>();","handlingStrategy":"type-guard","validationCode":"if (!typeof(TService).IsAssignableTo(manager.ServiceBaseType)) throw new InvalidOperationException(\"Wrong manager\");","typeGuard":"static bool ResolvableFrom<TBase>(Type t) => typeof(TBase).IsAssignableFrom(t);","tryCatchPattern":"try { var svc = manager.Get(serviceType); }\ncatch (ArgumentException ex) { logger.Error(ex, \"Type {Type} not assignable to manager base\"); }","preventionTips":["Prefer the generic Get<T>() overload so assignability is compile-checked.","Keep track of which ServiceManager<T> instance owns which hierarchy.","Avoid Type-based lookups when a generic call is possible."],"tags":["dependency-injection","type-mismatch","csharp"],"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"}