{"record":{"id":"e1b1840bd55c5377","repo":"dotnet/wpf","slug":"sr-morethanoneattachedannotation","errorCode":null,"errorMessage":"SR.MoreThanOneAttachedAnnotation","messagePattern":"SR\\.MoreThanOneAttachedAnnotation","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs","lineNumber":217,"sourceCode":"        /// The HighlightComponent uses built-in highlight rendering, so no transformation is needed\n        /// </summary>\n        /// <param name=\"transform\">Transform to the AnnotatedElement.</param>\n        /// <returns>Transform to the annotation component</returns>\n        public GeneralTransform GetDesiredTransform(GeneralTransform transform)\n        {\n            return transform;\n        }\n\n        /// <summary>\n        /// Add an attached annotation to the component. The attached anchor will be used to add\n        /// a highlight to the appropriate TextContainer\n        /// </summary>\n        /// <param name=\"attachedAnnotation\">The attached annotation to be added to the component</param>\n        public void AddAttachedAnnotation(IAttachedAnnotation attachedAnnotation)\n        {\n            if (_attachedAnnotation != null)\n            {\n                throw new ArgumentException(SR.MoreThanOneAttachedAnnotation);\n            }\n\n            //fire trace event\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordAnnotation, EventTrace.Event.AddAttachedHighlightBegin);\n\n            //check input data and retrieve the TextContainer\n            ITextContainer textContainer = CheckInputData(attachedAnnotation);\n\n            TextAnchor textAnchor = attachedAnnotation.AttachedAnchor as TextAnchor;\n\n            //Get highlight Colors from the cargo. For undefined Colors the default values are used\n            GetColors(attachedAnnotation.Annotation, out _background, out _selectedBackground);\n            _range = textAnchor;\n\n            Invariant.Assert(textContainer.Highlights != null, \"textContainer.Highlights is null\");\n\n            //get or create AnnotationHighlightLayer in the textContainer\n            AnnotationHighlightLayer highlightLayer = textContainer.Highlights.GetLayer(typeof(HighlightComponent)) as AnnotationHighlightLayer;","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs#L199-L235","documentation":"HighlightComponent supports at most one attached annotation at a time: it keeps a single _attachedAnnotation/_range pair for the highlight. AddAttachedAnnotation throws ArgumentException with SR.MoreThanOneAttachedAnnotation when a second attached annotation is added while one is still held.","triggerScenarios":"Calling AddAttachedAnnotation on a HighlightComponent (directly or via AnnotationComponentManager.AddToAttachedAnnotations) when _attachedAnnotation is already non-null — i.e. the previous attached annotation was not removed first.","commonSituations":"Applying the same annotation resource twice to overlapping ranges resolved to the same component; annotation store re-delivery/duplicate resolution during service reload; failing to call RemoveAttachedAnnotation during teardown before re-adding.","solutions":["Call RemoveAttachedAnnotation for the existing attached annotation before adding a new one.","Deduplicate annotation resources before applying so the same component never receives two annotations.","Catch ArgumentException to detect duplicates and skip/merge instead of adding.","Inspect the component's current attached annotation (if exposed) and choose modify/update logic rather than add."],"exampleFix":"// before\ncomponent.AddAttachedAnnotation(attachedAnnotation); // throws if one exists\n// after\nif (component.HasAttachedAnnotation)\n    component.RemoveAttachedAnnotation(component.CurrentAttachedAnnotation);\ncomponent.AddAttachedAnnotation(attachedAnnotation);","handlingStrategy":"validation","validationCode":"if (component.CurrentAttachedAnnotation != null)\n    component.RemoveAttachedAnnotation(component.CurrentAttachedAnnotation);\ncomponent.AddAttachedAnnotation(attachedAnnotation);","typeGuard":"bool CanAdd(IAnnotationComponent c) => c is HighlightComponent hc && !hc.HasAttachedAnnotation;","tryCatchPattern":"try { component.AddAttachedAnnotation(a); }\ncatch (ArgumentException) { /* duplicate: already has an attached annotation */ }","preventionTips":["Deduplicate annotation resources before applying","Always remove before re-adding","Apply annotations through AnnotationService so lifecycle is managed"],"tags":["wpf","annotations","duplicate","invalid-argument"],"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"}