{"record":{"id":"5f931084e3a5c3e2","repo":"dotnet/wpf","slug":"sr-servicetypealreadyadded","errorCode":null,"errorMessage":"SR.ServiceTypeAlreadyAdded","messagePattern":"SR\\.ServiceTypeAlreadyAdded","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/ServiceProviders.cs","lineNumber":47,"sourceCode":"        #endregion\n\n        /// <summary>\n        /// Add a new service\n        /// </summary>\n        /// <param name=\"serviceType\"></param>\n        /// <param name=\"service\"></param>\n        public void AddService(Type serviceType, Object service)\n        {\n            ArgumentNullException.ThrowIfNull(serviceType);\n            ArgumentNullException.ThrowIfNull(service);\n\n            if (!_objDict.ContainsKey(serviceType))\n            {\n                _objDict.Add(serviceType, service);\n            }\n            else if (_objDict[serviceType] != service)\n            {\n                throw new ArgumentException(SR.ServiceTypeAlreadyAdded, nameof(serviceType));\n            }\n        }\n\n        private Dictionary<Type,Object> _objDict = new Dictionary<Type,Object>();\n    }\n}\n","sourceCodeStart":29,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Markup/ServiceProviders.cs#L29-L54","documentation":"ServiceProviders.AddService throws ArgumentException(SR.ServiceTypeAlreadyAdded) when AddService is called for a serviceType that is already registered with a different service instance. The service map is write-once per type. This prevents silently replacing an already-published service object.","triggerScenarios":"Calling AddService(typeof(X), serviceA) and then AddService(typeof(X), serviceB) on the same ServiceProviders instance; re-registering a service during a second initialization pass; multiple components each attempting to register the same service type.","commonSituations":"Double initialization of a designer/serializer context (e.g. adding the same service to an EditorContext twice); shared service providers reused across parse/serialize operations; DI-style code assuming Add behaves like a dictionary indexer set.","solutions":["Check GetService(serviceType) for null before calling AddService and skip if already present.","Register each service type exactly once during initialization.","Use the same service instance if re-registration is intentional (the check passes when the stored instance equals the new one).","Catch ArgumentException and treat it as 'already registered' if idempotent registration is desired."],"exampleFix":"// before\nproviders.AddService(typeof(IMyService), new MyService());\nproviders.AddService(typeof(IMyService), new MyService()); // throws\n// after\nif (providers.GetService(typeof(IMyService)) == null)\n    providers.AddService(typeof(IMyService), new MyService());","handlingStrategy":"validation","validationCode":"bool canAdd = providers.GetService(typeof(IMyService)) == null;","typeGuard":"null","tryCatchPattern":"try { providers.AddService(typeof(IMyService), svc); }\ncatch (ArgumentException) { /* already registered with a different instance */ }","preventionTips":["Register each service type exactly once in a single init path.","Check GetService before AddService for idempotent registration.","Avoid re-running service registration on repeated initialization."],"tags":["wpf","xaml","service-provider","duplicate-registration"],"backgroundTag":"conflicting-config-options","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}