{"record":{"id":"8f092d7d36c9a106","repo":"LykosAI/StabilityMatrix","slug":"service-of-type-type-is-already-registered-with-a-different","errorCode":null,"errorMessage":"Service of type {type} is already registered with a different lifetime.","messagePattern":"Service of type (.+?) is already registered with a different lifetime\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Services/ServiceManager.cs","lineNumber":93,"sourceCode":"            }\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))\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        {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Services/ServiceManager.cs#L75-L111","documentation":"RegisterScoped<TService>(Func<IServiceProvider,T>) throws ArgumentException when TService already has an instance or provider registered (a 'different lifetime' conflict). Unlike the first check, the second failure (scopedProviders.TryAdd) would mean an existing scoped registration, reported by the 'already registered as Scoped' message.","triggerScenarios":"Calling RegisterScoped<TService>(provider) when TService was previously registered as Singleton/Transient via Register (instances/providers contain the key).","commonSituations":"Registering the same service both as a plain singleton and scoped during setup; migration from an older Register call to RegisterScoped without removing the old line.","solutions":["Remove the earlier non-scoped registration for TService","Pick one lifetime per service type and keep it consistent","Order registration so scoped services are only ever registered via RegisterScoped"],"exampleFix":"// before\nmanager.Register<ISession>(() => new Session());\nmanager.RegisterScoped<ISession>(sp => new Session()); // throws\n// after\nmanager.RegisterScoped<ISession>(sp => new Session()); // single lifetime","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(\"different lifetime\"))\n{\n    logger.LogError(ex, \"ISession already registered with a non-scoped lifetime\");\n}","preventionTips":["Assign exactly one lifetime per service type","Search the codebase for prior Register calls before adding RegisterScoped","Document each service's lifetime in the DI setup module"],"tags":["di","duplicate-registration","lifetime-conflict"],"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"}