{"record":{"id":"7c3f0b557775fa54","repo":"dotnet/wpf","slug":"sr-missingannotationhighlightlayer","errorCode":null,"errorMessage":"SR.MissingAnnotationHighlightLayer","messagePattern":"SR\\.MissingAnnotationHighlightLayer","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs","lineNumber":594,"sourceCode":"\n            Invariant.Assert(textContainer != null, \"TextAnchor does not belong to a TextContainer\");\n\n\n            //Get highlight Colors from the cargo and update the highlight layer\n            Color background, activeBackground;\n            GetColors(args.Annotation, out background, out activeBackground);\n\n            if (!_background.Equals(background) ||\n                !_selectedBackground.Equals(activeBackground))\n            {\n                //modify the highlight\n                Invariant.Assert(textContainer.Highlights != null, \"textContainer.Highlights is null\");\n\n                //get AnnotationHighlightLayer in the textContainer\n                AnnotationHighlightLayer highlightLayer = textContainer.Highlights.GetLayer(typeof(HighlightComponent)) as AnnotationHighlightLayer;\n                if (highlightLayer == null)\n                {\n                    throw new InvalidDataException(SR.MissingAnnotationHighlightLayer);\n                }\n\n                //change the colors and invalidate\n                _background = background;\n                _selectedBackground = activeBackground;\n                highlightLayer.ModifiedRange(this);\n            }\n        }\n\n        /// <summary>\n        /// Invalidating the measure on all the children (which are Shapes)\n        /// causes them to recalculate their desired geometry\n        /// </summary>\n        private void InvalidateChildren()\n        {\n            // Invalidating the measure on all the children (which are Shapes)\n            // causes them to recalculate their desired geometry\n            foreach (Visual child in Children)","sourceCodeStart":576,"sourceCodeEnd":612,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs#L576-L612","documentation":"HighlightComponent.OnAnnotationUpdated throws InvalidDataException(SR.MissingAnnotationHighlightLayer) when the text container's Highlights collection has no AnnotationHighlightLayer registered for typeof(HighlightComponent). The component expects the layer to have been created during Enable/Disable of the HighlightComponent on the AnnotationService; finding the layer null means the internal registration did not happen or was removed.","triggerScenarios":"Calling SetColors/update paths (OnAnnotationUpdated after modifying highlight resources) when the service was disabled or the component was never enabled on the TextContainer; calling UpdateLayers/annotation update after the AnnotationService was disconnected from the viewer.","commonSituations":"Disabling the AnnotationService (or navigating away from the document) while an asynchronous annotation update still fires; reusing HighlightComponent instances across TextContainers; custom AnnotationStore code that clears the Highlights layer.","solutions":["Keep the AnnotationService enabled (EnableHighlight/EnableAnnotationService) for the lifetime of any pending annotation updates.","Re-enable the component on the new TextContainer after navigation before raising annotation updates.","Catch InvalidDataException around annotation-update handling and re-enable/re-attach the service as a recovery step."],"exampleFix":"// before\nannotation.ModifyAsync(); // may fire update after service disabled\n// after\nif (annotationService.IsEnabled)\n    annotation.ModifyAsync();\nelse\n    annotationService.Enable(annotationService.HighlightComponent);","handlingStrategy":"try-catch","validationCode":"if (!annotationService.IsEnabled) return; // skip updates while disabled","typeGuard":null,"tryCatchPattern":"try { annotation.Update(); } catch (InvalidDataException) { annotationService.Disable(); annotationService.Enable(component); }","preventionTips":["Keep the AnnotationService enabled until pending async updates complete","Re-enable components on every TextContainer change (navigation)","Do not clear the Highlights layer manually"],"tags":["wpf","annotations","invalid-data-exception","internal-state"],"backgroundTag":"internal-invariant-violation","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"}