dotnet/wpf · error · InvalidDataException

SR.MissingAnnotationHighlightLayer

Error message

SR.MissingAnnotationHighlightLayer

What it means

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.

Solutions

  1. Keep the AnnotationService enabled (EnableHighlight/EnableAnnotationService) for the lifetime of any pending annotation updates.
  2. Re-enable the component on the new TextContainer after navigation before raising annotation updates.
  3. Catch InvalidDataException around annotation-update handling and re-enable/re-attach the service as a recovery step.

Example fix

// before
annotation.ModifyAsync(); // may fire update after service disabled
// after
if (annotationService.IsEnabled)
    annotation.ModifyAsync();
else
    annotationService.Enable(annotationService.HighlightComponent);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!annotationService.IsEnabled) return; // skip updates while disabled

Try / catch

try { annotation.Update(); } catch (InvalidDataException) { annotationService.Disable(); annotationService.Enable(component); }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7c3f0b557775fa54. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Component/HighlightComponent.cs:594

            Invariant.Assert(textContainer != null, "TextAnchor does not belong to a TextContainer");


            //Get highlight Colors from the cargo and update the highlight layer
            Color background, activeBackground;
            GetColors(args.Annotation, out background, out activeBackground);

            if (!_background.Equals(background) ||
                !_selectedBackground.Equals(activeBackground))
            {
                //modify the highlight
                Invariant.Assert(textContainer.Highlights != null, "textContainer.Highlights is null");

                //get AnnotationHighlightLayer in the textContainer
                AnnotationHighlightLayer highlightLayer = textContainer.Highlights.GetLayer(typeof(HighlightComponent)) as AnnotationHighlightLayer;
                if (highlightLayer == null)
                {
                    throw new InvalidDataException(SR.MissingAnnotationHighlightLayer);
                }

                //change the colors and invalidate
                _background = background;
                _selectedBackground = activeBackground;
                highlightLayer.ModifiedRange(this);
            }
        }

        /// <summary>
        /// Invalidating the measure on all the children (which are Shapes)
        /// causes them to recalculate their desired geometry
        /// </summary>
        private void InvalidateChildren()
        {
            // Invalidating the measure on all the children (which are Shapes)
            // causes them to recalculate their desired geometry
            foreach (Visual child in Children)

View on GitHub (pinned to 81131a70a4)