{"record":{"id":"e7f5a398f1995dfa","repo":"LykosAI/StabilityMatrix","slug":"service-of-type-type-is-already-registered-as-scoped","errorCode":null,"errorMessage":"Service of type {type} is already registered as Scoped.","messagePattern":"Service of type (.+?) is already registered as Scoped\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/ServiceManager.cs","lineNumber":98,"sourceCode":"\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))\n                throw new ArgumentException(\n                    $\"Service of type {type} is already registered with a different lifetime.\"\n                );\n\n            if (!scopedProviders.TryAdd(type, sp => provider(sp))) // Store as base type T\n                throw new ArgumentException($\"Service of type {type} is already registered as Scoped.\");\n        }\n\n        return this;\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(Type type, Func<IServiceProvider, T> provider)\n    {\n        lock (providers)\n        {\n            if (instances.ContainsKey(type) || providers.ContainsKey(type))\n                throw new ArgumentException(\n                    $\"Service of type {type} is already registered with a different lifetime.\"\n                );\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ServiceManager.cs#L80-L116","documentation":"RegisterScoped<TService> throws this second ArgumentException when instances/providers are clear but scopedProviders.TryAdd fails — meaning TService is already registered as scoped with another factory. It distinguishes lifetime conflicts from duplicate scoped registrations.","triggerScenarios":"Calling RegisterScoped<TService>(provider) twice, or RegisterScoped<TService> after RegisterScoped(typeof(TService), otherProvider) already stored a factory.","commonSituations":"DI setup method invoked twice (e.g. on re-login or test fixture re-initialization); two modules each registering their own factory for the same scoped interface.","solutions":["Register each scoped service exactly once","If a module may run twice, guard registration with a bool or ContainsKey check","Choose a single owner for the registration of shared services"],"exampleFix":"// before\nmanager.RegisterScoped<ISession>(sp => new Session());\nmanager.RegisterScoped<ISession>(sp => new Session(sp.GetUser())); // throws\n// after\nif (!manager.IsRegistered<ISession>())\n    manager.RegisterScoped<ISession>(sp => new Session(sp.GetUser()));","handlingStrategy":"validation","validationCode":"if (!manager.IsRegistered<ISession>())\n    manager.RegisterScoped<ISession>(sp => new Session(sp));","typeGuard":null,"tryCatchPattern":"try { manager.RegisterScoped<ISession>(provider); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already registered as Scoped\"))\n{\n    logger.LogDebug(\"ISession already scoped-registered; skipping\");\n}","preventionTips":["Ensure registration code runs once (guard with a bool flag)","Give each scoped interface exactly one owning module","Idempotency-test the DI bootstrap in unit tests"],"tags":["di","duplicate-registration","scoped-lifetime"],"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"}