{"record":{"id":"08f6614a379e3532","repo":"dotnet/wpf","slug":"sr-duplicateeventname","errorCode":null,"errorMessage":"SR.DuplicateEventName","messagePattern":"SR\\.DuplicateEventName","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs","lineNumber":70,"sourceCode":"            Type handlerType,\n            Type ownerType)\n        {\n            ArgumentNullException.ThrowIfNull(name);\n\n            if (routingStrategy != RoutingStrategy.Tunnel && \n                routingStrategy != RoutingStrategy.Bubble &&\n                routingStrategy != RoutingStrategy.Direct) \n            {\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"routingStrategy\", (int)routingStrategy, typeof(RoutingStrategy));\n            }\n\n            ArgumentNullException.ThrowIfNull(handlerType);\n\n            ArgumentNullException.ThrowIfNull(ownerType);\n\n            if (GlobalEventManager.GetRoutedEventFromName(name, ownerType, false) != null)\n            {\n                throw new ArgumentException(SR.Format(SR.DuplicateEventName, name, ownerType)); \n            }\n\n            return GlobalEventManager.RegisterRoutedEvent(name, routingStrategy, handlerType, ownerType);\n        }\n\n        /// <summary>\n        ///     See overloaded method for details\n        /// </summary>\n        /// <remarks>\n        ///     handledEventsToo defaults to false\n        ///     See overloaded method for details\n        /// </remarks>\n        /// <param name=\"classType\"/>\n        /// <param name=\"routedEvent\"/>\n        /// <param name=\"handler\"/>\n        /// <ExternalAPI/>\n        public static void RegisterClassHandler(\n            Type classType,","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs#L52-L88","documentation":"EventManager.RegisterRoutedEvent enforces that a routed event name is unique per owner type. If GlobalEventManager.GetRoutedEventFromName(name, ownerType, false) already returns an event, it throws ArgumentException with SR.DuplicateEventName. Registering the same (name, ownerType) pair twice would otherwise create ambiguous event lookups.","triggerScenarios":"Calling RegisterRoutedEvent with the same name and ownerType more than once — typically static registration code that runs twice (e.g. in both a static ctor and a module initializer), or registering in a base class and an identical name in the derived owner type reusing the same ownerType.","commonSituations":"Unit tests re-running static registration; assembly loaded twice in different contexts causing static initializers to double-register; copy-pasted registration code in two types with the same name/ownerType arguments.","solutions":["Use RoutedEvent.Register only once, typically in a static readonly field","Look up with EventManager.GetRoutedEvent(name, ownerType) and reuse if non-null before registering","Ensure the type is not loaded/initialized twice in the app domain"],"exampleFix":"// before\npublic static readonly RoutedEvent MyEvent =\n    EventManager.RegisterRoutedEvent(\"My\", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyControl));\n// duplicate second call with same name+ownerType throws\n// after\npublic static readonly RoutedEvent MyEvent =\n    EventManager.GetRoutedEvent(\"My\", typeof(MyControl))\n    ?? EventManager.RegisterRoutedEvent(\"My\", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyControl));","handlingStrategy":"validation","validationCode":"if (EventManager.GetRoutedEvent(name, ownerType) == null) { /* safe to register */ }","typeGuard":null,"tryCatchPattern":"try { evt = EventManager.RegisterRoutedEvent(name, RoutingStrategy.Bubble, h, ownerType); } catch (ArgumentException) { evt = EventManager.GetRoutedEvent(name, ownerType); }","preventionTips":["Register routed events once in static readonly fields","Guard re-registration with EventManager.GetRoutedEvent lookups","Avoid duplicate static initialization (double assembly load) scenarios"],"tags":["wpf","routed-events","duplicate","registration"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}