{"record":{"id":"67b3dc25c767085e","repo":"LykosAI/StabilityMatrix","slug":"service-of-type-type-is-already-registered-for-typeof-t","errorCode":null,"errorMessage":"Service of type {type} is already registered for {typeof(T)}","messagePattern":"Service of type (.+?) is already registered for (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/ServiceManager.cs","lineNumber":74,"sourceCode":"                    $\"Service of type {typeof(TService)} is already registered for {typeof(T)}\"\n                );\n            }\n\n            // Return type is wrong during build with method group syntax\n            // ReSharper disable once RedundantCast\n            providers[typeof(TService)] = () => (TService)provider();\n        }\n\n        return this;\n    }\n\n    public void Register(Type type, Func<T> providerFunc)\n    {\n        lock (providers)\n        {\n            if (instances.ContainsKey(type) || providers.ContainsKey(type))\n            {\n                throw new ArgumentException($\"Service of type {type} is already registered for {typeof(T)}\");\n            }\n\n            providers[type] = providerFunc;\n        }\n    }\n\n    /// <summary>\n    /// Register a new service provider action with Scoped lifetime.\n    /// The factory is called once per scope.\n    /// </summary>\n    public IServiceManager<T> RegisterScoped<TService>(Func<IServiceProvider, TService> provider)\n        where TService : T\n    {\n        var type = typeof(TService);\n\n        lock (providers)\n        {\n            if (instances.ContainsKey(type) || providers.ContainsKey(type))","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ServiceManager.cs#L56-L92","documentation":"ServiceManager<T>.Register(Type, Func<T>) throws ArgumentException when the given type already has an instance or provider registered under the base type T. It keeps the instances/providers dictionaries one-to-one with service types.","triggerScenarios":"Calling manager.Register(typeof(IFoo), () => new Foo()) when typeof(IFoo) already exists in instances or providers from any prior Register call.","commonSituations":"Reflection-driven registration loops that run more than once; registering both via typeof(Interface) and typeof(Implementation) keys where one collides; duplicated setup in plugin loading.","solutions":["Deduplicate the registration list before looping Register(Type, ...)","Track already-registered types in the caller and skip them","Use TryAdd-style semantics if replacement is desired (wrap in try/catch and continue on this specific ArgumentException)"],"exampleFix":"// before\nforeach (var t in types) manager.Register(t, () => Create(t)); // second run throws\n// after\nforeach (var t in types)\n    if (!manager.IsRegistered(t)) manager.Register(t, () => Create(t));","handlingStrategy":"validation","validationCode":"foreach (var t in types)\n    if (!manager.IsRegistered(t))\n        manager.Register(t, CreateFactory(t));","typeGuard":null,"tryCatchPattern":"try { manager.Register(type, providerFunc); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already registered\"))\n{\n    logger.LogDebug(\"{Type} already registered; ignoring\", type);\n}","preventionTips":["Deduplicate registration lists before looping","Use one canonical key type (interface, not implementation) per service","Track registered types in the loader to skip repeats"],"tags":["di","duplicate-registration","dependency-injection"],"backgroundTag":"conflicting-config-options","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"}